Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between rhino-mocks stub and expect

What is the difference between rhino-mocks stub and expect here: Looks to me that they behave exact the same?

mockContext.Stub(x => x.Find<Blog>())
    .Return(new List<Blog>() 
    { 
        new Blog() { Id = 1, Title = "Test" } 
    }.AsQueryable());

mockContext.Expect(x => x.Find<Blog>())
    .Return(new List<Blog>()
    {
        new Blog(){Id = 1,Title = "Title"},
        new Blog(){Id=2,Title = "no"}
    }.AsQueryable());
like image 898
Nikos Avatar asked Nov 30 '12 13:11

Nikos


People also ask

What is the difference between stubs and mocks?

Stubbing, like mocking, means creating a stand-in, but a stub only mocks the behavior, but not the entire object. This is used when your implementation only interacts with a certain behavior of the object.

What is a stub in mocking?

Stub: Stub is an object that holds predefined data and uses it to answer calls during tests. Such as: an object that needs to grab some data from the database to respond to a method call. Mocks: Mocks are objects that register calls they receive.

What is the difference between stubs and mocks in Ruby testing?

The difference between mocks and stubs A Test Stub is a fake thing you stick in there to trick your program into working properly under test. A Mock Object is a fake thing you stick in there to spy on your program in the cases where you're not able to test something directly.

What is stub and spy?

Spies are almost the opposite of stubs. They allow the doubled entity to retain its original behavior while providing information about how it interacted with the code under test. The spy can tell the test what parameters it was given, how many times it was called, and what, if any, the return value was.


2 Answers

Stub() defines the behavior for stubbed object.
Expect() defines the behavior and the expectation for mocked object.

So if you need to check that mocked method was called you should use Expect:

var mockContext = MockRepository.GenerateMock<IContext>();
mockContext.Expect(x => x.Find<Blog>()).Return(new List<Blog>());

Now after test action complete you are able to verify that expectaions are met:

mockContext.VerifyAllExpectations();

If you need to stub method behavior you can use Stub():

var mockContext = MockRepository.GenerateStub<IContext>();
mockContext.Stub(x => x.Find<Blog>()).Return(new List<Blog>());
like image 77
Alexander Stepaniuk Avatar answered Sep 29 '22 00:09

Alexander Stepaniuk


When you use Expect for a method in this case x.Find(), if your method is not called during the test mockContext.VerifyAllExpectations(); will fail.

like image 30
nnnn Avatar answered Sep 29 '22 00:09

nnnn