سؤال

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

error-->Unable to cast object of type 'Castle.Proxies.ObjectProxy_2' to type 'Vetserve.Messages.Shared.ICommand'.

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

Hi when i try to test using nunit test this method has previous error how can i fix it

this is my test class

 [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 fakeCxxommand = A.Fake<ICommandFetcher>();
        var browser = new Browser(with =>
        {
            with.Module<CommandModule>();
            with.Dependency<ICommandBus>(fakeCommand);
            with.Dependency<ICommandFetcher>(fakeCxxommand);
        });

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

        A.CallTo(() => fakeCommand.Send(A<ICommand>.Ignored , envelope.MetaData)).MustHaveHappened();
       // A.CallTo(() => fakeCommand.Send(A<ICommand>.Ignored, A<MetaData>._)).MustHaveHappened();
    }
هل كانت مفيدة؟

المحلول

[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 fakeCommandFetcher = A.Fake<ICommandFetcher>();
        A.CallTo(()=> fakeCommandFetcher.FetchFrom(A<MessageEnvelope>._)).Returns(rCommand);
        var browser = new Browser(with =>
        {
            with.Module<CommandModule>();
            with.Dependency<ICommandBus>(fakeCommand);
            with.Dependency<ICommandFetcher>(fakeCommandFetcher);
        });

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

        A.CallTo(() => fakeCommand.Send(rCommand,A<MetaData>.That.Matches(m =>m.ContextInfo == envelope.MetaData.ContextInfo && m.MessageType == envelope.MetaData.MessageType))).MustHaveHappened();

}

this is my new test method it work fine. previous error occur due to wrong type of fetch from method

 public interface ICommandFetcher
{
    Object FetchFrom(MessageEnvelope messageEnvelope);
}

i changed it into

 public interface ICommandFetcher
{
    ICommand FetchFrom(MessageEnvelope messageEnvelope);
}

now its working.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top