Asserting that a method is called exactly one time

后端 未结 7 1475
无人共我
无人共我 2021-02-05 03:16

I want to assert that a method is called exactly one time. I\'m using RhinoMocks 3.5.

Here\'s what I thought would work:



        
7条回答
  •  忘掉有多难
    2021-02-05 03:41

    I have been using the AssertWasCalled extension to get around this problem. This is the best I could find/come up with but it would be better if I didn't have to specify the call twice.

        [Test]
        public void just_once()
        {
            var key = "id_of_something";
    
            var source = MockRepository.GenerateStub();
    
            // set a positive expectation
            source.Expect(x => x.GetSomethingThatTakesALotOfResources(key))
                .Return(new Something())
                .Repeat.Once();
    
            var client = new Client(soure);
            client.GetMeMyThing(key);
            client.GetMeMyThing(key);
    
            source.AssertWasCalled(x => x.GetSomethingThatTakesALotOfResources(key),
                                   x => x.Repeat.Once());
            source.VerifyAllExpectations();
        }
    

提交回复
热议问题