I am trying to test the Register action in the customer controller of the Nop.Web project. The issue I am having is with the HttpSessionStateBase _sessionItems. How do I mock the _sessionItems so that its not null and it returns a value when a call is made to get a value. I tried to mock the FakeHttpSessionState class provided in the Nop.Core.Fakes which takes SessionStateItemCollection as its argument(since the FakeHttpSessionState class doesn't have a default constructor). But SessionStateItemCollection is a sealed class so I can't mock that class. I am doing something wrong. Is there a better way to do this. Please help. Thanks in advance.
I am testing the Customer Controller Nop.Web project. The action is Register with parameters. In that action there is a call to private method 'TryAssociateAccountWithExternalAccount'. RetrieveParametersFromRoundTrip is being called in that method. In that method a HttpSessionStateBase is obtained but the session items in HttpSessionStateBase object are null. So I am trying to figure out how to add a new session item. I tried to mock the FakeHttpSessionState class provided in the Nop.Core.Fakes which takes SessionStateItemCollection as its argument(since the FakeHttpSessionState class doesn't have a default constructor). But SessionStateItemCollection is a sealed class so I can't mock that class. How else can I add a session item???
I did create a default constructor but I don't see how is it going to help because the HttpSessionStateBase object is being returned by the FakeHttpContext class which calls the FakeHttpSessionStateBase contructor with a parameter and not the default one. Could you please elaborate a little bit more??
I did create a default constructor but I don't see how is it going to help because the HttpSessionStateBase object is being returned by the FakeHttpContext class which calls the FakeHttpSessionStateBase contructor with a parameter and not the default one. Could you please elaborate a little bit more??
I think I follow you kchugh. I don't know exactly how you have setup your test but I'll show you how to get a very basic test working based on your requirements. Also I'm using moq because I'm faster at writing examples in moq.
[Test] publicvoid AnotherTest() { //Setup the mocked session class var sessionStateMock = new Mock<HttpSessionStateBase>();
//Setup the mocked return sessionStateMock.Setup(m => m["nop.externalauth.parameters"]).Returns(23);
//Mock an engine context - This might be different for you, depends on how you have setup your test. Mock<IEngine> mockedEngine = new Mock<IEngine>(); mockedEngine.Setup(m => m.Resolve<HttpSessionStateBase>()).Returns(sessionStateMock.Object);
//Replace the current engine because you need your specially mocked classes. EngineContext.Replace(mockedEngine.Object);
//TODO: Here iswhere you assert your behaviors. Assert.AreEqual(23, EngineContext.Current.Resolve<HttpSessionStateBase>()["nop.externalauth.parameters"]); }
I hope this helps, but with the lack of information on how your tests are setup it is hard to give an exact example. The example above could break other parts of your tests depending on your setup.
Thanks skyler.severns a lot. I was trying to mock the HttpSessionStateBase class earlier too and trying to add a session item to it but was doing it wrong way. Then I tried it this way after looking up on google which worked :)
var mockObject = MockRepository.GeneratePartialMock<Object>(); var session = MockRepository.GenerateStub<HttpSessionStateBase>(); session["nop.externalauth.parameters"] = mockObject;
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.