Multiple calls to Mocked NonPublic method failing

2 posts, 0 answers
  1. Petter
    Petter avatar
    2 posts
    Member since:
    Oct 2017

    Posted 31 Oct 2017 Link to this post

    So I have a method that I'm trying to test that looks something like the following:

            public void MethodToTest(List<Object> importEntities, short id)
            {
                var relevantObjects = importEntities.Select(some code).Distinct();

                var list1= new Dictionary<string, NewTenantRange>();

                var list2= new Dictionary<string, NewTenantRange>();

                var list3= new Dictionary<string, NewTenantRange>();

                foreach (var obj in relevantObjects)
                {
                    if (!list1.ContainsKey(obj.key))
                    {
                        var item1 = NewTenantRange.GetBwTenantRange(obj);
                        list1.Add(item1.Number, item1);
                    }

                    if (!list2.ContainsKey(obj.key))
                    {
                        var item2 = NewTenantRange.GetBbTenantRange(obj);
                        list2.Add(item2.Number, item2);
                    }

                    if (!list3.ContainsKey(obj.key))
                    {
                        var item3 = NewTenantRange.GetVsTenantRange(obj);
                        list3.Add(item3.Number, item3);
                    }
                }

                InsertRange(list1.Values, tenantId, Ranges.Range1);
                InsertRange(list2.Values, tenantId, Ranges.Range2);
                InsertRange(list3.Values, tenantId, Ranges.Range3);
            }

    And my test so far:

    [TestMethod]
            public void TestMethodToTest()
            {
                //Arrange
                var service = Mock.Create<myService>(Constructor.Mocked, Behavior.CallOriginal);

                var entities = FakeLf10Orders().ToList(); //returns list of fake objects

                Mock.NonPublic.Arrange(service, "InsertRange",
                        ArgExpr.IsAny<IEnumerable<TenantRangeService.NewTenantRange>>(), Arg.AnyShort, Arg.IsAny<Ranges>())
                    .DoNothing().Occurs(3);

                //Act 
                service.MethodToTest(entities, Arg.AnyShort);

                //Assert
                Mock.AssertAll(service);
            }

    Now, everuthing is working fine until the method InsertRange is called for the second time. Then instead of DoNothing(), the code is executed and therefore fails. Anyone with any ideas?

     

    Thank you!

    //Petter

  2. Petter
    Petter avatar
    2 posts
    Member since:
    Oct 2017

    Posted 06 Nov 2017 in reply to Petter Link to this post

    Never mind, figured it out.

    It was the last parameter in the Assert that was incorrect:

    Arg.IsAny<Ranges>()  should be ArgExpr.IsAny<Ranges>().

     

     

     

     

Back to Top