Mocking Dictionary.TryGetValue

4 posts, 0 answers
  1. JP
    JP avatar
    2 posts
    Member since:
    Mar 2019

    Posted 28 Mar 2019 Link to this post

    Hi,

     

    I'm trying to mock a Dictionary.TryGetValue method based on what's described in this thread to no avail.

    What I've tried so far:

    var dictionary = Mock.Create<Dictionary<string, HashSet<SomeClass>>>();
     
    HashSet<SomeClass> expected = new HashSet<SomeClass>();
    expected.Add(new SomeClass(param1, param2));
    Mock.Arrange(() => dictionary.TryGetValue(Arg.AnyString, out expected)).Returns(true);

    However, when ever I run the test, I get a null in the out object.

    To give some context, not sure it impacts or not, the test spans a service running in a separate thread. The mocked dictionary is successfully inserted via a PrivateAccessor. 

    Thanks in advance,

    JP

     

  2. Ivo
    Admin
    Ivo avatar
    32 posts

    Posted 29 Mar 2019 Link to this post

    Hello Juan,

    I have tested the following method and it seems to work as expected:

    internal class SomeClass
    {
        private object param1;
        private object param2;
     
        public SomeClass(object param1, object param2)
        {
            this.param1 = param1;
            this.param2 = param2;
        }
    }
     
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            // Arrange
            var dictionary = Mock.Create<Dictionary<string, HashSet<SomeClass>>>();
            HashSet<SomeClass> expected = new HashSet<SomeClass>();
            var param1 = new object();
            var param2 = new object();
            expected.Add(new SomeClass(param1, param2));
            Mock.Arrange(() => dictionary.TryGetValue(Arg.AnyString, out expected)).Returns(true);
     
            // Act
            HashSet<SomeClass> actual = null;
            var result = dictionary.TryGetValue("AnyKey", out actual);
     
            // Assert
            Assert.AreEqual(expected, actual);
        }
    }


    Could you please describe your use case in more details, so I could be able to reproduce the issue, it is possible that I am missing something. 


    Regards,
    Ivo
    Progress Telerik
    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 Feedback Portal and vote to affect the priority of the items

  3. JP
    JP avatar
    2 posts
    Member since:
    Mar 2019

    Posted 29 Mar 2019 in reply to Ivo Link to this post

    Hi Ivo,

    Thanks for your reply.

    Just to reproduce the issue, I simplified the test to exactly the same one you implemented above, and I still got a null hashset as a result.

    I then simply copied your test class from above into my environment, and got the following exceptions:

    Managed Debugging Assistant 'BindingFailure' occurred
    Message=Managed Debugging Assistant 'BindingFailure' : 'The assembly with display name 'xunit.assert' failed to load in the 'LoadFrom' binding context of the AppDomain with ID 3.
    The cause of the failure was: System.IO.FileNotFoundException: Could not load file or assembly 'xunit.assert' or one of its dependencies. The system cannot find the file specified.'

     

    Telerik.JustMock.Core.ElevatedMockingException occurred
      HResult=0x80131500
      Message=Cannot mock 'System.Collections.Generic.Dictionary`2[System.String,System.Collections.Generic.HashSet`1[SomeClass]]'. The profiler must be enabled to mock, arrange or execute the specified target.
      Source=Telerik.JustMock
      StackTrace:
       at Telerik.JustMock.Core.ProfilerInterceptor.ThrowElevatedMockingException(MemberInfo member)
       at Telerik.JustMock.Core.MocksRepository.Create(Type type, MockCreationSettings settings)
       at Telerik.JustMock.Mock.<>c__38`1.<Create>b__38_0()
       at Telerik.JustMock.Core.ProfilerInterceptor.GuardInternal[T](Func`1 guardedAction)
       at Telerik.JustMock.Mock.Create[T]()
       at TestMethod1() in

    I enabled the profiler (as indicated by the second exception), and all worked as expected. No really sure why the first exception is coming, but it works nonetheless.

    Thanks for your help!

  4. Ivo
    Admin
    Ivo avatar
    32 posts

    Posted 29 Mar 2019 Link to this post

    Hello Juan,

    I am glad to hear that your issue is now resolved. If you have further questions do not hesitate to contact us again.

    Regards,
    Ivo
    Progress Telerik
    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 Feedback Portal and vote to affect the priority of the items
Back to Top