简体   繁体   English

编写方法的NUnit测试用例

[英]Writing NUnit Test Cases for method

i am new for Nunit.please help for write a test case. 我是Nunit的新手,请帮助编写一个测试用例。 this is my class 这是我的课

    public CommandModule(ICommandFetcher fetcher,ICommandBus commandBus)
    {

        //Get["/"] = p =>
        //{z
        //    return Response.AsText((string)Request.Form.Username);
        //};

        Post["/"] = parameters =>
        {
            var commandRequest = this.Bind<MessageEnvelope>();
            var command = fetcher.FetchFrom(commandRequest);
            commandBus.Send((ICommand)command, commandRequest.MetaData);
            return HttpStatusCode.OK;
        };
    }
}

and i want to test for check this method 我想测试检查此方法

  commandBus.Send((ICommand)command, commandRequest.MetaData);

thank you! 谢谢!

i try it following way 我按照以下方式尝试

 [Test]
    public void whern_reseiving_command_it_sent_to_the_command_bus()
    {

        var rCommand = new DummyCommand() { SomeProp = 2 };
        var serializedCommand = JsonConvert.SerializeObject(rCommand);
        var envelope = new MessageEnvelope() { MetaData = new MetaData() { MessageType = "DummyCommand", MessageTypeVersion = 1 }, MessageData = serializedCommand };
        var fakeCommand = A.Fake<ICommandBus>();

        var browser = new Browser(with =>
        {
            with.Module<CommandModule>();
            with.Dependency<ICommandBus>(fakeCommand);

        });

        var result = browser.Post("/", with =>
        {
            with.HttpRequest();
            with.JsonBody(envelope);
        });

        A.CallTo(() => fakeCommand.Send(rCommand,envelope.MetaData)).MustHaveHappened();

but A.CallTo(() => fakeCommand.Send(rCommand,envelope.MetaData)).MustHaveHappened(); 但是A.CallTo(() => fakeCommand.Send(rCommand,envelope.MetaData)).MustHaveHappened(); it has some kind of error in rcommand value 它在rcommand值中存在某种错误

It sounds like you are looking to explicitly test that ICommandBus.Send is called when your code is executed. 听起来您正在寻找显式测试在执行代码时是否调用了ICommandBus.Send方法。

One approach is to mock the ICommandBus dependency. 一种方法是模拟ICommandBus依赖关系。 That is, insert a mock object implementing ICommandBus that is able to detect whether that method is called with the right parameter values. 即,插入一个实现ICommandBus的模拟对象,该对象能够检测是否使用正确的参数值调用了该方法。

If you take this approach, you would normally do this using a mocking framework (eg Moq, or RhinoMocks). 如果采用这种方法,通常可以使用模拟框架(例如Moq或RhinoMocks)来进行。

To explain how you would use a mock for this briefly, I will show how you can do this by by explicitly implementing a mock object object yourself that records method calls, and testing them afterwards. 为了简要说明如何使用模拟,我将展示如何通过自己明确实现一个模拟对象对象(记录方法调用)并随后对其进行测试来实现此目的。

Eg 例如

MockCommandBus : ICommandBus
{
    ICommand PassedCommand { get; set; }
    MetaData PassedMetaData { get; set; }

    public void Send(ICommand command, MetaData metaData)
    {
        this.PassedCommand = command;
        this.PassedMetaData = metaData;
    }    
}

Then your test case will look like this: 然后您的测试用例将如下所示:

[TestCase]
public void PostSendsCommandOnBus()
{
    // ARRANGE
    var mockCommandBus = new MockCommandBus();

    ICommand expectedCommand = <whatever you expect>;
    MetaData expectedMetaData = <whatever you expect>;

    // Code to construct your CommandModule with mockCommandBus.

    // ACT
    // Code to invoke the method being tested.

    // ASSERT
    Assert.AreEqual(expectedCommand, mockCommandBus.PassedCommand);
    Assert.AreEqual(expectedMetaData , mockCommandBus.PassedMetaData );

}

Caveat: 警告:

Note that this is only one approach to unit testing that happens to fit exactly what you are asking for here. 请注意,这只是一种恰好符合您在此处要求的单元测试方法。 Excessive use of mocks to test explicit interaction with dependencies at a low level can lead to the development of very brittle test suites that are testing an underlying implementation rather than behaviour of a system. 过度使用模拟来测试与依赖项之间的显式交互,这可能导致开发非常脆弱的测试套件,这些套件正在测试基础实现而不是系统的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM