Fabii
Fabii

Reputation: 3890

Why use Rhino Mock Stub method?

I just started using Rhino mock to set up test cases for my project. What exactly does the .Return(objToReturn:list) do ?

Its only seems to work if I initialize and populate the list then pass it to the mock stub method. I was a assuming I could used the Mock Stub method to populate a list then return that populated list.

....
    private ProductRepository _productRepository;
    private IProductRepository _productRepositoryStub;

    [SetUp]
    public void SetUp()
    {
        _productRepository = new ProductRepository();


        //Testing using Rhino Mocks
        //Generate stub
         _productRepositoryStub = MockRepository.GenerateMock<IProductRepository>();
    }

    [Test]
    public void Canquerydb()
    {
        IList list = _productRepository.GetAllProducts();
        _productRepository.Stub(x=> x.GetAllProducts()).Return(list);
        _productRepositoryStub.AssertWasCalled(x => x.GetAllProducts());
    }


    /// <summary>
    /// Refaactor and use MockRhino here
    /// </summary>
    [Test]
    public void can_insert_product()
    {
        IProduct product = new Grains("Cheese Bread", "Dairy grain", 0);
        _productRepository.SaveProduct(product);
        _productRepositoryStub.Stub(x=>x.SaveProduct(product));
        _productRepositoryStub.AssertWasCalled(x => x.SaveProduct(product));

    }

Upvotes: 3

Views: 12109

Answers (1)

Nick
Nick

Reputation: 6588

To answer the title of your question: Rhino mocks makes a distinction between Mocks and Stubs. A Mock is the only thing that can make a test fail because it is a wrapped instance of the thing you are testing - your System Under Test (SUT). You "stub" something in Rhino mocks so that you can satisfy the dependencies of your mocked object. The stub gives you visibility of the arguments you pass to it, and gives you control of the return result so that you can make assertions about the behaviour of your Mock in complete isolation.

This site has further explanation of Mocks and Stubs.

To add, Libraries like Moq do not make the distinction between Mocks and Stubs. Through the use of generics you don't have to Mock your SUT. I personally prefer this simplicity of Moq.

Upvotes: 4

Related Questions