Use rhino mocks

Source: Internet
Author: User

Rhino mocks is a very important component in TDD (test-driven development ).ArticleThis section briefly describes how to use the mock component. Create two projects,

  1. Mock for abstract classes
    Add productbase class Using System;

    NamespaceRhinomockproject
    {
    Public Abstract ClassProductbase
    {
    Public Abstract StringName {Get;Set;}

    Public Abstract DecimalPrice {Get;Set;}

    Public Abstract VoidSave ();
    }

    [testmethod]
    Public void teststubabstract ()
    {< br> productbase Product = mockrepository. generatestub ();
    product. name = " laptop computer " ;
    product. price = 3200.00 m ;

    Assert. areequal (3200.00 m, Product. Price );
    }

  2. Mock OF THE INTERFACE Public Interface Iproduct
    {
    String Name { Get ; Set ;}

    DecimalPrice {Get;Set;}
    }

    UsingSystem;
    UsingSystem. Collections. Generic;
    UsingSystem. LINQ;
    UsingSystem. text;

    namespace rhinomockproject
    {< br> Public class productmanager
    {< br> Public static void doubleprice (iproduct product)
    {< br> product. price * = 2 ;
    }< BR >}

    [testmethod]
    Public void teststubinterface ()
    {< br> decimal price = 3200.00 m ;
    iproduct Product = mockrepository. generatestub ();
    product. name = " laptop computer " ;
    product. price = price;

    Productmanager. doubleprice (product );
    Assert. areequal (price *2, Product. Price );
    }

  3. For mock with the return value method, Make sure to mock the objects with returned values,Mockrepository must be instantiated Using System;
    Using System. Collections. Generic;
    Using System. LINQ;
    Using System. text;

    NamespaceRhinomockproject
    {
    Public InterfaceIproductrepository
    {
    Iproduct get (IntProductid );

    Ienumerable <iproduct> select ();

    BoolSave (iproduct product );
    }
    }

    Private Ienumerable <iproduct> _ fakeproducts = New List <iproduct>
    {
    New Product {name = " Steak " , Price = 9.85 m },
    New Product {name = " Milk " , Price = 2.02 m },
    New Product {name = " Diapers " , Price = 33.07 m }
    };

    [testmethod]
    Public void teststubinterfacemethod ()
    {< br> mockrepository mocks = New mockrepository ();
    iproductrepository products = mocks. stub ();

    Using(Mocks. Record ())
    {
    Setupresult. For (products. Select (). Return (_ fakeproducts );
    }

    VaRResults = products. Select ();

    Assert. areequal (3, Results. Count ());
    }

    multiple mock return values [testmethod]
    Public void teststubmultiplereturn ()
    {< br> mockrepository mocks = New mockrepository ();
    iproductrepository products = mocks. stub ();

    using (mocks. record ()
    {< br> setupresult
    . for (products. get ( 2 )
    . return ( New product {name = " beer " , price = 12.99 m });

    setupresult
    . for (products. get ( 12 )
    . return ( New product {name = " steak " , price = 8.02 m });
    }

    // test
    iproduct product1 = products. get ( 2 );
    assert. areequal ( " beer " , product1.name);

    Iproduct product2 = products. Get (12);
    Assert. areequal ("Steak", Product2.name );

    Iproduct product3 = products. Get (13);
    Assert. isnull (product3 );
    }

    [Testmethod]
    Public VoidTeststubignorearguments ()
    {
    Mockrepository mocks =NewMockrepository ();
    Iproductrepository products = mocks. Stub <iproductrepository> ();

    Using (Mocks. Record ())
    {
    Setupresult
    . For (products. Get ( 1 ))
    . Ignorearguments ()
    . Return ( New Product {name = " Beer " , Price =12.99 m });
    }

    //Test
    Iproduct product1 = products. Get (2);
    Assert. areequal ("Beer", Product1.name );

    Iproduct product2 = products. Get (12);
    Assert. areequal ("Beer", Product2.name );
    }

    This article briefly introduces the application of the rahino mock component in TDD,CodeDownload: http://files.cnblogs.com/jackhuclan/ConsoleApplication6.rar
    Complete example, see http://stephenwalther.com/archive/2008/03/23/tdd-introduction-to-rhino-mocks.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.