Rhino Mocks断言方法被调用一次

时间:2016-07-07 15:10:33

标签: c# unit-testing rhino-mocks objectfactory

我有一个方法,我正在使用Rhino Mocks进行测试。当我自己测试这个方法时,它会通过,但是当测试多个方法时,它会失败。我认为这是因为我没有正确地破坏某些东西。下面是我创建ObjectFactory的设置代码。

public void Setup()
    {
        ObjectFactory.Initialize(x =>
        {
            x.For<ISecurityManager>().Use<SecurityManager>();
            x.For<IManager>().Use<Manager>();
            x.For<IWorkflowManager>().Use<WorkflowManager>();
        });
    }

以下是其中一个单元测试的示例。

    var mockIWorkflowManager = MockRepository.GenerateMock<IWorkflowManager>();
    mockIWorkflowManager.Stub(d => d.SendCompletedEmailWithComments(Arg<WorkOrder>.Is.Anything, Arg<string>.Is.Anything));
    ObjectFactory.Inject(typeof(IWorkflowManager), mockIWorkflowManager);

    SubmitAction action = new SubmitAction();
    action.SendNotifyEmail("toUser", new WorkOrder(), "currentUser", new StepAction { Name = "with comments" });

    mockIWorkflowManager.AssertWasCalled(x => x.SendCompletedEmailWithComments(Arg<WorkOrder>.Is.Anything, Arg<string>.Matches(y => y == "toUser")));
    mockIWorkflowManager.AssertWasNotCalled(x => x.SendNotifyEmail(Arg<string>.Is.Anything, Arg<WorkOrder>.Is.Anything, Arg<string>.Is.Anything));

其他测试类似,只是更改返回值以影响逻辑路径。如果我一次一个地运行它们,它们都会通过,但是如果我一次运行它们,它们都会失败。

0 个答案:

没有答案