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

I'm having trouble setting up a mocked HttpSession where I have different keys and want to return the same value for each key. Here is a rough break down of the method I am testing:

            String previousaction = (String)context.Session["previous action"];
            if (previousaction == null || previousaction.Length == 0)
                Log("Previous action not valid");
                return false;

            // We weren't called from ShowLogin. So do that instead of this.
            if (!previousaction.Equals("show login"))
                context.Session.Remove("previous action");
                return false;

            // Check that this is not the fourth attempt at logging in and that
            // the account is not locked.
            int attempts = 0;
            if (context.Session["attempts"] != null)
                attempts = (int)context.Session["attempts"];

So I always want to return the same value for a given parameter. This
is my current test setup for the state (I've got the context stuff setup as per my previous post):

// Setup a valid post - these are correct user credentials
            NameValueCollection formvars = new NameValueCollection();
            formvars.Add("USERNAME", "proctos");
            // This is wrong - need the right password before running this method!!
            formvars.Add("PASSWORD", "testing");
            this.mockHttpRequest.ExpectGetAlways("Form", formvars);

            // Setup a valid session.
            // We originally showed the login form. Now we are accepting login details
            this.mockHttpSessionState.ExpectGetIndexAlways("show login").Args("previous action");
            this.mockHttpSessionState.ExpectGetIndexAlways("accept login").Args("action");

Trouble is, when the test runs I'm getting errors because I don't think I'm using ExpectGetIndexAlways correctly.

My aim is to be able to setup a session environment tailored to a code path and then be able to test that the correct code path executes, the correct session vars are set and cleared (and so on).

Any advice?

Ta, S
asked by simonpro (1.8k points)

3 Answers

0 votes
8) This is what Conditional Expectations are for.
Using this you can mock according to passed arguments.
The only thing is that it is an Exterprise Feature.
Here is how
this.mockHttpSessionState.ExpectGetIndexAlways("show login").When("previous action");
this.mockHttpSessionState.ExpectGetIndexAlways("accept login").When("action");

:idea: You could use DynamicReturnValues to return this but it will make your code less readable
answered by scott (32k points)
0 votes
Ahh so close! Of course its obvious to me now!!

Thanks a lot for this - its a big help. In case you can't tell, I've only been using Typemock for about a day :)
answered by simonpro (1.8k points)
0 votes
If you are only using TypeMock for a day, you are doing really well.
There are many other features and I hope that our documentation is good enough.
Of course we would like to hear if you have any comments, suggestions or criticism.
answered by scott (32k points)