0

I am trying to assert that a method was called in MSpec while using NSubstitute as the mocking framework. The Subject has an empty implementation of ExecuteAll(). It should fail, but it passes!

public class When_told_to_execute_all_it_should_execute_all_commands 
   : WithSubject<CommandHandler>
{
    static AddTaskCommand command1;
    static AddTaskCommand command2;

    Establish context = () => 
    {
        command1 = An<AddTaskCommand>();
        command2 = An<AddTaskCommand>();
        Subject.AcceptCommand(command1);
        Subject.AcceptCommand(command2);
    };

    Because of = () => Subject.ExecuteAll();

    It should_have_called_execute_on_command_1 = () => command1.Received().Execute();
    It should_have_called_execute_on_command_2 = () => command2.Received().Execute();
}

I tried using a different assertion, but it passes, too! It seems that this is itself calling into Execute()

command1.WasToldTo(x => x.Execute());

I removed NSubstitute and replaced it with Moq. After fixing the error about making Execute() virtual, it worked. Is NSubstitute bugged or not reporting errors correctly?

Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
4imble
  • 13,979
  • 15
  • 70
  • 125
  • 1
    Since you gave me -1 without saying anything I am not sure how you want me to improve this question. I believe I have explained in well written English what I have tried, what i am trying to achieve and provided some code. If I am missing something please let me know. Just throwing a -1 in there does not help anyone at all. – 4imble Nov 03 '14 at 10:04
  • Your question is actually pretty good. I tweaked the title and some formatting. I think the important question is how NSubstitute is handling that (non)virtual method! – Anthony Mastrean Nov 04 '14 at 18:54
  • Thanks, you are right, but I did not know that was the problem initially :). Thanks for clarifying it now that we know more. – 4imble Nov 06 '14 at 09:57

1 Answers1

0

Okay, after Moq erroring that the method was not virtual I put NSubstitute back in and it's working. If i change the method back to non virtual it is passing when it shouldnt be again.

I have created an issue for NSubstitute, would be nice if it gave an error like moq.

4imble
  • 13,979
  • 15
  • 70
  • 125