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
Hi,

I'm trying to test some ASP .NET code in NUnit. When I try to instantiate an HttpSessionState variable with the following code:

this.mockHttpSessionState = MockManager.MockObject(typeof(HttpSessionState), Constructor.Mocked, new object[] { null });

this.httpSessionState = (HttpSessionState)mockHttpSessionState.Object; 



I'm getting a compile error that says 'TypeMock.Mock' does not contain a definition for 'Object'. Has this been changed in the latest TypeMock version? Any suggestions into how to solve this problem would be greatly appreciated.

Thanks very much,

VictoriaBC :)
________
Honda CB175 specifications
asked by VictoriaBC (760 points)

10 Answers

0 votes
Please post the definition of mockHttpSessionState.
It might be a Mock instead of a MockObject.
answered by scott (32k points)
0 votes
Hi,

can you check up the definition of this.mockHttpSessionState?
I think it defined as Mock when it should be define as MockObject
answered by lior (13.2k points)
0 votes
Thanks for the quick reply!

Yes, it was defined as Mock instead of a MockObject, so I changed the definition to MockObject.

Now when I'm trying to add to the sessionstate like this:

httpSessionState.Add("test", "test");


I'm getting a null reference exception---not set to an instance of an object when I try to use the this.httpSessionState variable and even when I instantiate this way

this.mockHttpSessionState = MockManager.MockObject(typeof(HttpSessionState), Constructor.Mocked);


it still gives me a null reference error when trying to run the code.

I'm wondering if I need to instantiate the HttpSessionState in a different way?


Thanks again,

VictoriaBC
________
Sambar
answered by VictoriaBC (760 points)
0 votes
Hi,

I think that youre getting the Null referance exeption becuase you are reaching "real" code on a mocked instance.

When you sepcify that the constructor is mocked The real construction will not be performed resulting in an uninitialized object. This will many times cause problems if later on unmocked calls will be made on this object since most of the inner members were not initialzied properly.

In any case if you can post the entire test code it will be easier to tell exactly what goes wrong. I have a feeling that you may be mocking the wrong instance there. (In most cases the class that is under test is not the one being mocked)
answered by lior (13.2k points)
0 votes
Hi Lior,

Here's the code....it doesn't even get past the "setup" stage, as the null reference exception happens as soon as I try to add to the empty httpSessionstate object.


[SetUp]
public void SetUp()
{
      MockManager.Init();
   
      this.mockHttpContext = MockManager.MockObject(typeof(HttpContext), Constructor.Mocked, new object[] { null });

      this.httpContext = (HttpContext)mockHttpContext.Object; 

      ConstructorInfo[] constructorInfos = typeof(HttpSessionState).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance);
           
      this.mockHttpSessionState = MockManager.MockObject(typeof(HttpSessionState), Constructor.Mocked, new object[] { null });

      this.httpSessionState = (HttpSessionState)mockHttpSessionState.Object;
            
      httpSessionState.Add("test", "testing");
}


[Test]
public void TestGetMyRecentProcesses()
{
      DataTable recentProcesses = ProcessInstanceUtility.GetMyRecentProcesses(user, httpSessionState);

      Assert.IsNotNull(recentProcesses);
}
            




Regards,

VictoriaBC
________
starcraft 2 replays
answered by VictoriaBC (760 points)
0 votes
Hi,

As I said the problem is that the call to:
httpSessionState.Add("test", "testing");

is not mocked. i.e the real code for add is executed. (by default all calls which are not mocked are executed normally)
this of course wont work since the underlying members of httpSessionState has not been properly initialized.

If you will add this:
mockHttpSessionState.ExpectCall("Add").Args("test", "testing");
The setup phase will pass.

:!: using our Tracer tool will help know which methods are being mocked and which ones are being executed by "real" code.
answered by lior (13.2k points)
0 votes
Hi Lior,

Thanks for the help. Just one more question, all that I'm trying to do now is pass an HttpSessionState object into a method:

[Test]
public void TestGetMyRecentProcesses()
{
      DataTable recentProcesses = ProcessInstanceUtility.GetMyRecentProcesses(user, httpSessionState);

      Assert.IsNotNull(recentProcesses);
} 


I'm not sure what method to use to mock a httpsessionstate that's supposed to be passed to an outside function. Is there a way to mock the HttpSessionState's values so that the method "GetMyRecentProcesses" will accept the mock HttpSessionState? Also, when I run the tracing tool, the add method was expected, but there was an unexpected get call whose origins I don't know. Your help is greatly appreciated by the way:)

Thanks,

VictoriaBC
________
Scion (automobile)
answered by VictoriaBC (760 points)
0 votes
Hi,

When you want ot pass a mocked object to an "outside" methods you use the MockObject class as you have done. However the object is not a real live object, the trick is to fake all calls made on that object from the "outside" method.

In your case you will need to look at "ProcessInstanceUtility.GetMyRecentProcesses" and see what calls it makes on the httpSessionState object and fake them.

Regarding the expectataion for the "Add" I dont see a place were you set it up, so it is strange (but then again I dont see the entire code so I might be missing something). If you can send me a more complete picture (i.e. the full test code) we can take it offline if you like.

:?: why do you not call MockManager.Verify at the end of your test?
answered by lior (13.2k points)
0 votes
Hi Lior,
I'm not sure if i need to mock the the first call "GetMyRecentProcesses" which is actually what I'm trying to test, to see if it returns all the processes. I just need to fake the httpSessionState being passed into it.

Here's the setup code and part of the test code which I'm using:

[SetUp]
        public void SetUp()
        {
            
            process = new Process(1);
            processVersion = new ProcessVersion(3);
            user = new User(25);
            user.Processes.Add(process);
            user.Save();

            processAlert = new ProcessAlert(1);
            processInstance = new ProcessInstance(1);
            provider = new Provider(1);

            provider.PdrProvider.Regions.Add(new ProviderDataRepository.Objects.Region(3));
            provider.PdrProvider.Regions.Add(new ProviderDataRepository.Objects.Region(39));
            provider.Save();
            providerId = provider.Id;

            Console.WriteLine("Process count: " + user.Processes.Count);

            MockManager.Init();

            this.mockHttpSessionState = MockManager.MockObject(typeof(HttpSessionState), Constructor.Mocked);
            this.httpSessionState = (HttpSessionState)mockHttpSessionState.Object;
                            
        }


   [Test]
   public void TestGetMyRecentProcesses()
   {
        
         Logic.Search.SortOrder sortOrder = new SortOrder(typeof(ProcessInstance), ProcessInstance.Field.StartDate, ODLB.Query.Order.Ascending);
         DataTable recentProcesses = new DataTable();

         Mock processMock = MockManager.Mock(typeof(ProcessUtility));
         processMock.ExpectAndReturn("processQueuedForDeletion", true).Args(process, httpSessionState);

         Mock sortMock = MockManager.Mock(typeof(Logic.State.Sorting));
         sortMock.ExpectAndReturn("GetSort", sortOrder).Args(httpSessionState, "Active", sortOrder);

        DataTable recentProcesses = ProcessInstanceUtility.GetMyRecentProcesses(user, httpSessionState);
        
        Assert.IsNotNull(recentProcesses);
        MockManager.Verify();
            



The HttpSessionState is being used as a parameter in two classes outside of the test case.

The error I get when I run Nunit is the following:
Credentials.Utilities.NUnitTests.ProcessInstanceUtilityTest.TestGetMyRecentProcesses : TypeMock.VerifyException : 
TypeMock Verification: Call to Credentials.Utilities.ProcessUtility.processQueuedForDeletion() Parameter: 1
    passed object [Credentials.Objects.Process]
    is not the same instance as expected [Credentials.Objects.Process]



Thanks,
victoriaBC
________
BMW N45 history
answered by VictoriaBC (760 points)
0 votes
Hi,

When you test "GetMyRecentProcesses" you dont need to mock it. (if you will the actual code wont execute)

The error you get is a problem with mismatching arguments. From what I gather you use the process created in the setup as the expected argument. However, when executing the "real" code the process passed is a new instance created from the data saved in the setup and not the same process instance. Since the two instances are different argument matching fails.

In order to solve this, you can use a custom check (see our guide under Cusotm Argument Checks).
answered by lior (13.2k points)
...