chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Production code:
private void instrumentCollectionViewControl_ClearAllInstrument(object sender, EventArgs e)
      {
         this.command = new CollectiveCommand(new ClearInstrumentCollectionCommand(), new DisplayProductSelectionCommand());
         this.command.Execute(this.Context);
      }


Test code:
[Test]
      public void instrumentCollectionViewControl_ClearAllInstrumentTest()
      {
         this.mockBasePage.ExpectGet("Context", this.httpContext);

         Mock mockClearInstrumentCollectionCommand = MockManager.Mock(typeof(ClearInstrumentCollectionCommand));
         mockClearInstrumentCollectionCommand.ExpectConstructor();

         Mock mockDisplayProductSelectionCommand = MockManager.Mock(typeof(DisplayProductSelectionCommand));
         mockDisplayProductSelectionCommand.ExpectConstructor();

         Mock mockCollectiveCommand = MockManager.Mock(typeof(CollectiveCommand));
         mockCollectiveCommand.ExpectConstructor().Args(Check.IsTypeOf(typeof(ClearInstrumentCollectionCommand)), Check.IsTypeOf(typeof(DisplayProductSelectionCommand)));
         mockCollectiveCommand.ExpectCall("Execute").Args(this.httpContext);

         base.InvokeMethod("instrumentCollectionViewControl_ClearAllInstrument", new object[]{null, null});
      }


Result in NUnit:

Rbsfm.VTradeWeb.Presentation.Views.instrumentpricerTest.instrumentCollectionViewControl_ClearAllInstrumentTest : System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
  ----> TypeMock.VerifyException : 
TypeMock Verification: Call to Rbsfm.VTradeWeb.Presentation.Core.FrontController.Commands.CollectiveCommand..ctor() 2 Parameters expected but recived 1
TearDown : TypeMock.VerifyException : 
TypeMock Verification: Method Rbsfm.VTradeWeb.Presentation.Core.FrontController.Commands.CollectiveCommand.Execute() has 1 more expected calls
Method Rbsfm.VTradeWeb.Presentation.Views.instrumentpricer.get_Context() has 1 more expected calls
asked by c0d3-m0nk3y (8.7k points)

5 Answers

0 votes
Hi,
I have tested this internally and it works as expected. Your test framework probably creates a new CollectiveCommand (I would guess in base.InvokeMethod)

The easiest way to find out is by running the TypeMock Tracer and seeing if and where the another constructor is begin called
answered by scott (32k points)
0 votes
This is the body of the InvokeMethod:
protected object InvokeMethod(string methodName, object[] args)
      {
         MethodInfo methodInfo = this.basePage.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
         return methodInfo.Invoke(this.basePage, args);
      }

As you can see it just reflects on my production class to invoke the protected method.

I ran the trace and only a single expectation was created for a CollectiveCommand - and the first time a constructor was called it failed the expectation :?
answered by c0d3-m0nk3y (8.7k points)
0 votes
Hi,

What you can do is to put a breakpoint at both the constructors and run with debug (using TestDriven.NET for example). You will see when the constructor is called and where from.
answered by scott (32k points)
0 votes
This isn't a bug - my bad! But I still have a question - my constructore for the collectivecommand is overloaded:

public class CollectiveCommand : BaseCommand
   {
      private ArrayList commands;
      
      public CollectiveCommand() : this(new ArrayList())
      {
         
      }

      public CollectiveCommand(ArrayList CommandArrayList) : base()
      {
         this.commands = CommandArrayList;
      }

      public CollectiveCommand(params ICommand[] args)
      {
         this.commands = new ArrayList();
         foreach(ICommand command in args)
         {
            this.commands.Add(command);
         }
      }

      protected override void OnExecute(HttpContext httpContext)
      {
         foreach (ICommand command in this.commands)
         {
            command.Execute(httpContext);
         }
      }
   }


As you see, I have a
params ICommand[] args
arg for the constructor so although I am passing 2 args in the constructor it will be coming into the constructor as an array - hence the expected 2 but recived 1 failure....

How do I test for this condition in ExpectConstructor()??
answered by c0d3-m0nk3y (8.7k points)
0 votes
c0d3-m0nk3y,

Here is how to do it:
object constructorParams = new object[] {
    Check.IsTypeOf(typeof(ClearInstrumentCollectionCommand)),
    Check.IsTypeOf(typeof(DisplayProductSelectionCommand)) };
mockCollectiveCommand.ExpectConstructor().Args(constructorParams );


:idea: I found this by searching the forums for params and I found Argument constriants
answered by scott (32k points)
...