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());
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.
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.
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.
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.
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>());
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With