Not returning an Arranged mock

6 posts, 0 answers
  1. Chuck
    Chuck avatar
    7 posts
    Member since:
    Feb 2012

    Posted 04 Feb 2013 Link to this post

    I have the following Arrange code
    var oRegMgr = Mock.Create<IRegionManager>();
    var oCmdRegion = Mock.Create<IRegion>();
    var oCmdView = Mock.Create<CommandingView>(Constructor.Mocked);
     
    Mock.Arrange(() => oRegMgr.Regions[RegionNames.CommandingRegion]).Returns(oCmdRegion);
    Mock.Arrange(() => oCmdRegion.ActiveViews.FirstOrDefault()).Returns(oCmdView);

    This calls a function that does this
    IRegion commandingRegion = oRegionManager.Regions[RegionNames.CommandingRegion];
    CommandingView oCmdView = (CommandingView)commandingRegion.ActiveViews.FirstOrDefault();

    (the region manager is returned via a previous call that supplies the mocked region manager, bu that is not germane here, so I have concentrated on the problem area. The access to the region manager sequence is used in several other unit tests, and works fine. When I step thru this in debug, the ProxyRegionManager is in fact the one being used).
    I get the mocked region back from oRegionManager just fine. However, when the call to get FirstOrDefault happens, it returns null, NOT the mocked CommandingView.
    Am I missing something in the Arrange for invoking FirstOrDefault?

    Thanks

  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 06 Feb 2013 Link to this post

    Hi Chuck,

    This thread has been replied in your support ticked:
    It appears to be a bug in JustMock, which we have already put in our backlog for future fix. You will be notified when we can provide you the solution.
     
    As a workaround to the matter, I could suggest the using of the original FirstOrDefault() implementation, but adding the desired default parameter inside the mocked object property, like this:
    var oRegMgr = Mock.Create<IRegionManager>();
    var oPrismSL = Mock.Create<IServiceLocator>();
    var oSL = Mock.Create<Ingr.SP3D.Common.Client.ServiceLocator>();
    var oCmdRegion = Mock.Create<IRegion>();
    var oCmdView = Mock.Create<CommandingView>(Constructor.Mocked);
    var oCmdViewModel = Mock.Create<CommandingViewModel>(Constructor.Mocked);
    var oXmlNode = Mock.Create<XmlNode>();
      
    oCmdRegion.ActiveViews.Add(oCmdView);
      
    Mock.SetupStatic(typeof(ClientServiceProviderS3), Behavior.Strict);
    Mock.Arrange(() => ClientServiceProviderS3.ServiceLocator).Returns(oSL);
    Mock.Arrange(() => oSL.Locator).Returns(oPrismSL);
    Mock.Arrange(() => oPrismSL.GetInstance<IRegionManager>()).Returns(oRegMgr);
    Mock.Arrange(() => oRegMgr.Regions[RegionNames.CommandingRegion]).Returns(oCmdRegion);
    Then when you call the original FirstOrDefault() method you should get the oCmdView returned.

    Please do not hesitate to contact us further if you need more help, concerning our product.

    Regards,
    Kaloyan
    the Telerik team
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  3. DevCraft R3 2016 release webinar banner
  4. Chuck
    Chuck avatar
    7 posts
    Member since:
    Feb 2012

    Posted 06 Feb 2013 Link to this post

    Hmmm. I wonder if ya'll noticed (as I just discovered) IViewsCollection does not have an Add method.
    Apparently Prism just loads up the collection behind the scenes and does not allow the caller to mess with the membership.
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 07 Feb 2013 Link to this post

    Hi Chuck,

    Thanks again for reaching out to us. 

    Since ActiveViews is a collection, the recommended way is to mock it via ReturnsCollection that will go in-depth and will let you execute FirstOrDefault or any other Enumerable helper methods on it.

    Here is a sample test that I came up with to clarify it further:



        [TestMethod]
        public void TestMethod1()
        {
            // Arrange
            var mock = Mock.Create<Test>();
     
            IList<View> fakesViews = new List<View>
            {
               new View { Name = "test" }
            };
           
            Mock.Arrange(() => mock.ActiveViews).ReturnsCollection(fakesViews);
     
            // Act
            var actual = mock.ActiveViews.FirstOrDefault();
     
            // Assert
            Assert.AreEqual("test", actual.Name);
        }
    }
     
    class Test
    {
        public List<View> ActiveViews { get; set; }
    }
     
    public class View
    {
        public string Name {get;set;}
    }

     

    Kind Regards
    Mehfuz
    the Telerik team
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  6. Chuck
    Chuck avatar
    7 posts
    Member since:
    Feb 2012

    Posted 08 Feb 2013 Link to this post

    I apologize for being dense, but I do not have ReturnsCollection as an available method (not an Expectation is I think the operative term). When I try to specify this code, ReturnsCollecton is not one of the possible methods. Adding it by hand gives compilation error.
    Mock.Arrange(() => oCmdRegion.ActiveViews). // want to add ReturnsCollection here, but no dice

    I am using Q3 2012 (2012.3.1016.3) version of JustMock

  7. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 12 Feb 2013 Link to this post

    Hi Chuck,

    I managed to configure a test that might work in your case, please give it a try:
    // ARRANGE
    // Here we mock IRegion in order to be able to arrange expectations later.
    IRegion mock = Mock.Create<IRegion>();
     
    // If you need you could use a mocked instance of the CommandingView class.
    CommandingView oCmdView = new CommandingView();
     
    // Here we add the oCmdView in our fake collection which needs to be from a ViewsCollection type. Later we will call the oCmdView with FirstOrDefault().
    ViewsCollection fakesViews = new ViewsCollection(new ObservableCollection<ItemMetadata>(new ItemMetadata[] { new ItemMetadata(oCmdView) }), im => true);
     
    // We arrange the mocks AvtiveViews property to return our fake collection.
    Mock.Arrange(() => mock.ActiveViews).ReturnsCollection(fakesViews);
     
    // ACT
    // We call the getter of the ActiveViews property with FirstOrDefault().
    var actual = mock.ActiveViews.FirstOrDefault();
     
    // ASSERT
    // We assert that the actual result should be the same as our oCmdView.
    Assert.AreEqual(oCmdView, actual);
    Note that, I am able to use ReturnsCollection() on my property.

    I hope this helps.

    Regards,
    Kaloyan
    the Telerik team
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Back to Top
DevCraft R3 2016 release webinar banner