0
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();
    }
Dariusz Woźniak
  • 9,640
  • 6
  • 60
  • 73
user3044294
  • 205
  • 1
  • 15
  • 1
    Please post the code of the Unit test as well, I feel `command` object you are passing in `CommandBus.Send` is wrong. – Carbine Nov 28 '13 at 11:01
  • thanks for comment i add the test class as well – user3044294 Nov 28 '13 at 11:06
  • 1
    What is the signature of FetchFrom? Also it seems like you would want to set up the fakeCxxCommand to return a particular object on FetchFrom - or not fake out the ICommandFetcher at all. – Christian Horsdal Nov 28 '13 at 11:48
  • I'm with @ChristianHorsdal. What's `FetchFrom`'s signature? I'm guessing it doesn't return an `ICommand`. – Blair Conrad Nov 28 '13 at 19:35
  • thanks Blair Conrad and Christain. FetchFrom is my deserilization method it use only for deserialization "commandRequest ". i changed the type in FetchFrom method object to Icommand now its working . Thank you! – user3044294 Nov 29 '13 at 04:52

1 Answers1

0
[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.

user3044294
  • 205
  • 1
  • 15