Rhino Mocks AssertWasCalled (multiple times) on property getter using AAA

前端 未结 6 1267
半阙折子戏
半阙折子戏 2020-12-30 18:20

I have a mocked object that is passed as a constructor argument to another object.

How can I test that a mocked object\'s property has been called? This is code I am

相关标签:
6条回答
  • 2020-12-30 18:49

    From Here:

    mock.AssertWasCalled(x => x.Name ="Bob");
    

    or

    mock.AssertWasCalled(x => x.Name =Arg.Is("Bob"));
    

    or

    mock.AssertWasCalled(x => x.Name =Arg<string>.Is.NotNull);
    
    0 讨论(0)
  • 2020-12-30 18:53

    I agree with chris answer

    newContact.AssertWasCalled(x => { var dummy = x.Forenames; }, options => options.Repeat.AtLeastOnce());
    

    Additionally If you know exactly how many times the property would be called you can do

    newContact.AssertWasCalled(x => { var dummy = x.Forenames; }, options => options.Repeat.Times(n));
    

    where n is an int.

    0 讨论(0)
  • 2020-12-30 18:54

    What is your motivation behind checking the number of times it is called? Is it a particularly expensive operation? If so, then I would suggest that you put it behind a method instead as, semantically speaking, properties should be inexpensive calls.

    Also, checking the number of times a property is called is not the thrust of unit testing (don't worry it's a common mistake to test too much, we've all been there). What you should really be testing is that given the state of your mock object that the method produces the expected output. The number of times a method is called to do that doesn't really matter (unless it's a service to send an email or something). It is an implementation detail which you normally wouldn't test as a simple refactor would break your tests as they would be too specific.

    0 讨论(0)
  • 2020-12-30 19:00

    newContact.Expect( c => c.ForeNames ).Return( ... ).Repeat.Any()

    0 讨论(0)
  • 2020-12-30 19:01

    Depending on your version of Rhino you are using, you can use:

    // Call to mock object here
    LastCall.IgnoreArguments().Repeat.Never();
    
    0 讨论(0)
  • 2020-12-30 19:10

    newContact.AssertWasCalled(x => { var dummy = x.Forenames; }, options => options.Repeat.AtLeastOnce());

    Repeat.Any does not work with AssertWasCalled because 0 counts as any... so if it WASN'T called, the AsserWasCalled would return TRUE even if it wasn't called.

    0 讨论(0)
提交回复
热议问题