简体   繁体   中英

Writing NUnit Test Cases for method

i am new for Nunit.please help for write a test case. 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(); it has some kind of error in rcommand value

It sounds like you are looking to explicitly test that ICommandBus.Send is called when your code is executed.

One approach is to mock the ICommandBus dependency. That is, insert a mock object implementing ICommandBus that is able to detect whether that method is called with the right parameter values.

If you take this approach, you would normally do this using a mocking framework (eg Moq, or 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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