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 mock session state within an unity injected WebAPI from this article.

http://www.strathweb.com/2012/11/adding ... t-web-api/

Unfortunately the HttpContext.Session.Current is always null?

Any tips? I'm also using unity to inject this into a ApiController?

Thanks,
Omar
asked by okhan (920 points)

8 Answers

0 votes
Hi Omar,

Can you please post the test code?
answered by ohad (35.4k points)
0 votes
Hi,

I've attached the code. The problems is that when it's invoked the SessionID is null.

Thanks,
Omar

SessionRepositoryTest.zip
answered by okhan (920 points)
0 votes
Hi Omar,

This is what you need to do in order to fake the SessionID:

        [Test]
        public void InstanceIdGetTest()
        {
            Isolate.WhenCalled(() => HttpContext.Current).ReturnRecursiveFake();
            var fakeSession = HttpContext.Current.Session;
            Isolate.WhenCalled(() => fakeSession.SessionID).WillReturn("1");
            Assert.AreEqual("1", fakeSession.SessionID);
        }



This way, the Session is faked because the HttpContext.Current is faked recursively.
After the session is faked, you can use the WhenCalled API in order to determine the sessionID value.

Please let us know if it helps.
answered by NofarC (4k points)
0 votes
Hi,

I'm able to get further, but still have issues.

I've attached the test code.

I'm experiencing some peculiar behavior on Asserts, that simply could be me not understanding how the assertations should work or my "Arrange" working.

The InstanceIdSetTest in the attached code passes, but the InstanceIdGetTest fails the Assert, but when I check the values they are equal in debug when I hover over the values, but throw the below:


Expected string length 1 but was 0. Strings differ at index 0.
Expected: "1"
But was: <string.Empty>
-----------^

at NUnit.Framework.Assert.That(Object actual, IResolveConstraint expression, String message, Object[] args)
at NUnit.Framework.Assert.AreEqual(Object expected, Object actual)
at UnitTests.SessionState.SessionRepositoryTest.InstanceIdGetTest() in C:ProjectsUnitTestsSessionStateSessionRepositoryTest.cs:line 54
at TypeMock.MockManager.getReturn(Object context, String typeName, String methodName, Object methodGenericParams, Boolean isDecorated, Boolean isInterceptedType, Object[] methodArguments)
at Typemock.Interceptors.Profiler.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected, Boolean isInterceptedType)
at UnitTests.SessionState.SessionRepositoryTest.InstanceIdGetTest() in C:ProjectsUnitTestsSessionStateSessionRepositoryTest.cs:line 52


PutInSessionTest & RemoveFromSessionTest, any idea how I can verify assert if they executed once?

System.InvalidOperationException : HttpContext.Current is not available, and is null
at SessionState.SessionRepository.PutInSession(String key, Object val) in C:ProjectsSessionStateSessionRepository.cs:line 53
at UnitTests.SessionState.SessionRepositoryTest.PutInSessionTest() in C:ProjectsSessionStateSessionRepositoryTest.cs:line 62
at TypeMock.MockManager.getReturn(Object context, String typeName, String methodName, Object methodGenericParams, Boolean isDecorated, Boolean isInterceptedType, Object[] methodArguments)
at Typemock.Interceptors.Profiler.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected, Boolean isInterceptedType)
at SessionState.SessionRepositoryTest.PutInSessionTest() in C:ProjectsUnitTestsSessionStateSessionRepositoryTest.cs:line 60

Also, GetFromSessionTest as the i'm getting the correct value back from the fake, but it fails the Assert even if they are equal.

Any suggestions on how to approach the above?

Thanks,
Omar

SessionRepositoryTest.zip
answered by okhan (920 points)
0 votes
Hi Omar,

Thank you for the feedback.
We'll look into this and answer ASAP.
answered by alex (17k points)
0 votes
Hi Omar,

First, can you please make the class SessionRepositoryTest public?
It seems to solve some issues when we tried reproducing the assert problems.

Second, Which runner do you use? and which Nunit version?


About the PutInSessionTest & RemoveFromSessionTest:
you can use Isolate.Verify.WasCalledWithAnyArguments() to verify if a method was executed.
You can read more about it here.
answered by NofarC (4k points)
0 votes
Hi,

Made the SessionRepositoryTest public, but unfortunately no improvement for me.

I'm currently using the "TMockRunner" within Visual Studio, I'm clicking on the shield with exclamation points (one doesn't have the exclamation point and is green, the InstanceIdSetTest)

Nunit is 2.6.0.12051 with .Net 4 runtime set

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!--
The .NET 2.0 build of the console runner only
runs under .NET 2.0 or higher. The setting
useLegacyV2RuntimeActivationPolicy only applies
under .NET 4.0 and permits use of mixed mode
assemblies, which would otherwise not load
correctly.
-->
<startup useLegacyV2RuntimeActivationPolicy="true">
<!-- Comment out the next line to force use of .NET 4.0 -->
<!--
<supportedRuntime version="v2.0.50727" />
-->
<supportedRuntime version="v4.0.30319" />
</startup>

<runtime>
<!-- Ensure that test exceptions don't crash NUnit -->
<legacyUnhandledExceptionPolicy enabled="1" />

<!-- Run partial trust V2 assemblies in full trust under .NET 4.0 -->
<loadFromRemoteSources enabled="true" />

<!-- Look for addins in the addins directory for now -->
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="lib;addins"/>
</assemblyBinding>

</runtime>

</configuration>
answered by okhan (920 points)
0 votes
I have an update, I downloaded 7.1.7, and 7.1.6 and have differing behavior. The Asserts still have an issue.

However, now I have to put the

Isolate.WhenCalled(() => HttpContext.Current).ReturnRecursiveFake();
var fakeSession = HttpContext.Current.Session;
Isolate.WhenCalled(() => fakeSession.SessionID).WillReturn("1");

in the test method outside of Setup, the assert still fails even if the value is the same but now I can't use setup in the same way before?

I'm keeping you posted so you may be able to diff the code to see if anything else could have changed between 7.1.6 & 7.1.7, I also would like an update on the above, as I'm not sure where to go with this.

Thanks,
Omar
answered by okhan (920 points)
...