BDD test when a Customer creates an user account

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 anni tempo fa
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.
12 anni tempo fa
Does anybody know the solution to my problem. Some help would be highly appreciated :)
12 anni tempo fa
What controller are you testing? CustomerController doesn't use sessions. Have you done any customization?
12 anni tempo fa
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???
12 anni tempo fa
kchugh wrote:
...since the FakeHttpSessionState class doesn't have a default constructor

Why not adding a default constructor to FakeHttpSessionState for these testing purposes?
12 anni tempo fa
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??
12 anni tempo fa
kchugh wrote:
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]
        public void 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 is where 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.
12 anni tempo fa
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.