سؤال

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());
هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top