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've been trying to mock an indexer on a class. A simple example would be the HttpSession.
context.Session[key] = value;

I expected to use a method like ExpectSetIndex but I couldn't figure out how to make it work. From what I could tell, the following code will fail because the second expectation replaces the first:
IIndexSetParameters stuff = this.mockHttpSessionState.ExpectSetIndex();
stuff.When("company", "thecompany");
stuff.When("department", "thedepartment");

What I want to be able to do is to make multiple calls. Something like this:
context.Session["company"] = "thecompany";
context.Session["department"] = "thedepartment";

So, in a nutshell, a class has an indexer and I want to mark out my expected sets and gets on that indexer. I did wonder about get_Item and set_Item but I had no luck with those either.

Any help appreciated. Thanks!
asked by simonpro (1.8k points)

4 Answers

0 votes
Hi

You should just call ExpectSetIndex two time
Like this:
mockHttpSessionState.ExpectSetIndex().When(("company", "thecompany"); 
mockHttpSessionState.ExpectSetIndex().When(("department", "thedepartment");

//use the indexer
context.Session["company"] = "thecompany"; 
context.Session["department"] = "thedepartment";


Hope it helps :D
answered by ohad (35.4k points)
0 votes
Hi,
Thanks Ohad,

Just a tip, To mock setting only specific indexes:
mockHttpSessionState.ExpectSetIndexAlways().When(Check.Or("company","department"),Check.IsAny());


To mock all gets of specific indexed use:
mockHttpSessionState.ExpectGetIndexAlways("thecompany").When("company");
mockHttpSessionState.ExpectGetIndexAlways("thedepartment").When("department");
answered by scott (32k points)
0 votes
Hi,

Thats just what I wanted. However, some of these keys will be set with temporal data. In these cases I'm more interested that the key is set and with the correct type rather than with a specific value.

So is it possible to say something along the lines of (psuedo code):

mockHttpSessionState.ExpectSetIndexUsing().When("somevalue", typeof(String));


Would this be a feature request??

Ta!!

S
answered by simonpro (1.8k points)
0 votes
Here you go:
mockHttpSessionState.ExpectSetIndexUsing().When("indexValue", Check.IsTypeOf(typeof(String)));
answered by scott (32k points)
...