This is a migrated thread and some comments may be shown as answers.

Making sure a method does something using justmock

2 Answers 66 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
ken
Top achievements
Rank 1
ken asked on 24 May 2011, 06:09 PM
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?

Thanks!!!

2 Answers, 1 is accepted

Sort by
0
ken
Top achievements
Rank 1
answered on 24 May 2011, 06:33 PM
OK - So I worked on this some more and this is what I came up with and I thought it would work.
[TestMethod]
    public void VendorManagementModule_WillCreateInstanceOfVendorMappingAdapter()
    {
      //Arrange
      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);
       
      //Act
      //vendorManagementModule.Initialize();
 
      //Assert
      Mock.Assert(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?

Thanks
0
Ricky
Telerik team
answered on 30 May 2011, 02:09 PM
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,
Mehfuz
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
Tags
General Discussions
Asked by
ken
Top achievements
Rank 1
Answers by
ken
Top achievements
Rank 1
Ricky
Telerik team
Share this question
or