Making sure a method does something using justmock

3 posts, 0 answers
  1. ken
    ken avatar
    14 posts
    Member since:
    Mar 2011

    Posted 24 May 2011 Link to this post

    Hi there,

    So I have been looking at some just mock examples but can't put together how we would verify that calling a particular method did something, i.e. called another method. Here is the snippet I want to test.
    public void Initialize()
         //Initialize the vendor mapping module
         container.RegisterInstance<VendorMappingViewModel>(new VendorMappingViewModel(eventAggregator));

    I want to make sure that contianer.RegisterInstance is called when Initialize is called. I can't test a variable cause it isn't like it stores something somewhere... Is there an example of this somewhere?

  2. ken
    ken avatar
    14 posts
    Member since:
    Mar 2011

    Posted 24 May 2011 Link to this post

    OK - So I worked on this some more and this is what I came up with and I thought it would work.
        public void VendorManagementModule_WillCreateInstanceOfVendorMappingAdapter()
          var mockEventAggregator = Mock.Create<IEventAggregator>();
          var mockRegionManager = Mock.Create<IRegionManager>();
          var mockContiner = Mock.Create<IUnityContainer>();
          Mock.Arrange(() => mockContiner.RegisterInstance<VendorMappingViewModel>(new VendorMappingViewModel(mockEventAggregator))).MustBeCalled();
          Mock.Arrange(() => mockContiner.Resolve<IEventAggregator>()).Returns(mockEventAggregator);
          Mock.Arrange(() => mockContiner.Resolve<IRegionManager>()).Returns(mockRegionManager);
          Mock.Arrange(() => mockEventAggregator.GetEvent<AddVendorMappingEvent>()).Returns(Mock.Create<AddVendorMappingEvent>());
          Mock.Arrange(() => mockEventAggregator.GetEvent<AddModuleTabEvent>()).Returns(Mock.Create<AddModuleTabEvent>());
          Mock.Arrange(() => mockRegionManager.RegisterViewWithRegion(Arg.IsAny<string>(), Arg.IsAny<Func<Object>>())).DoNothing();
          Mock.Arrange(() => mockEventAggregator.GetEvent<ShowVendorManagementWindowEvent>()).Returns(Mock.Create<ShowVendorManagementWindowEvent>());
          Mock.Arrange(() => mockEventAggregator.GetEvent<ShowReturnPolicyManagementWindowEvent>()).Returns(Mock.Create<ShowReturnPolicyManagementWindowEvent>());
          var vendorManagementModule = new VendorManagementModule(mockContiner);

    The main thing that I care about is the like where I am saying RegisterInstance MustBeCalled(). as you can see the Act portion of the test is commented out and it should fail cause nothing else is calling that but is always passes. What am I missing?

  3. Ricky
    Ricky avatar
    467 posts

    Posted 30 May 2011 Link to this post

    Hi ken,
    Thanks for sending the post. Here Mock.Assert is not throwing the expected exception since mockContainer.RegisterInstance is not invoked with VendorMappingViewModel instance that is used to set it up.

    Therefore, you could write it using Any matcher in the following way:
    Mock.Arrange(() => mockContiner.RegisterInstance<VendorMappingViewModel>(Arg.IsAny<VendorMappingViewModel>())).MustBeCalled();

    That will make it assert properly.

    Kind Regards,
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top