chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
So, I have this little piece of code

public class DataElephant
{

public static DataElephant Instance
        {
            get
            {
                DataElephant elephant =  (DataElephant) HttpContext.Current.Application["Elephant"];
                if (elephant != null)
                {
                    return elephant;
                }
                else
                {
                    elephant = new DataElephant();
                    HttpContext.Current.Application["Elephant"] = elephant;
                    return elephant;
                }
            }
        }
}



The test code goes something like this:

[Test]
        [VerifyMocks]
        public void DataElephantInstanceNewTest()
        {
            MockObject<HttpContext> mockedContext = MockManager.MockObject<HttpContext>(Constructor.Mocked);
            
            Mock httpContextMock = MockManager.Mock<HttpContext>(Constructor.Mocked);
            httpContextMock.ExpectGetAlways("Current", mockedContext.MockedInstance);

            MockObject<HttpApplicationState> mockedApplication = MockManager.MockObject<HttpApplicationState>(Constructor.Mocked);

            mockedContext.ExpectGet("Application", mockedApplication.MockedInstance);
            mockedApplication.ExpectGetIndex(null).Args("Elephant");

            Mock elephantMock = MockManager.Mock<DataElephant>();
            
            mockedApplication.ExpectSetIndexAlways().Args("Elephant", elephantMock.MockedInstance );
            

            DataElephant elephant = DataElephant.Instance;
            
        }


Expect that won't work, of course, since elephantMock.MockedInstance is null until the code is actually ran. If I could somehow mock the DataElephant constructor and return a mock object...
asked by M. Johansson (2.2k points)

3 Answers

0 votes
Hi,

Actually this test passes ok for me.
but I can see why you are confused.

Let me dig a little deeper into it and see what exactly can be done with this.
one possiblity that you might want to conside is to replace this expectation:
mockedApplication.ExpectSetIndexAlways().Args("Elephant", elephantMock.MockedInstance);

with
mockedApplication.ExpectSetIndexAlways().Args("Elephant", Check.NotNull());
answered by lior (13.2k points)
0 votes
M.
You are testing a singleton which can be quite tricky. :-)

:idea: Using Natural Mocks makes this simpler

[Test, VerifyMocks]
public void Instance_WhenNotInApplicationData_WillCreateANewInstance()
{
    using (RecordExpectations r = RecorderManager.StartRecording())
    {
        // Application State will have no reference to elephant
        r.ExpectAndReturn(HttpContext.Current.Application["Elephant"], null);

        // A new Elephant will be created
        DataElephant fakeElephant= new DataElephant();

        // Application State will be set with new elephant
        HttpContext.Current.Application["Elephant"] = fakeElephant;
    }
    
    DataElephant elephant = DataElephant.Instance;

    // Lets see if the elephant is a new elephant (it will be mocked if it is)
    Assert.IsTrue(InstanceIsMocked(elephant));
}

public bool InstanceIsMocked(object instance)
{
    foreach(Mock mock in MockManager.GetInstanceMocks(instance.GetType()))
    {
        if (object.ReferenceEquals(instance, mock.MockedInstance))
            return true;
    }
    return false;
}


:arrow: Note, There is a missing functionality from Typemock.
1. There is not a nice way to verify that a new instance was created
2. There is no internal way to Assert that an instance is mocked
answered by eli (5.7k points)
0 votes
Very satsfied with the answer, thank you!

Fast and professional!
answered by M. Johansson (2.2k points)
...