Mocking the IHttpSessionState

70 views
Skip to first unread message

philipp...@gmail.com

unread,
Apr 30, 2008, 2:32:11 PM4/30/08
to MoQ Discussions
Hi

I'm using WCSF and I'm trying to mock the access to the session (I
think the same apply to MVC). An IHttpContext interface exposes the
Session property. Naively, I'm trying to set-up my mock with the
following code

Guid something = Guid.Empty;
Mock<IHttpSessionState> sessionMock = new
Mock<IHttpSessionState>();
sessionMock.ExpectSet<object>(session =>
session["UserId"]).Callback(x => something = (Guid)x);

When executed, I get the following exception: Moq.MockException:
Expression is not a property access: session.get_Item("UserId")

The IHttpSessionState (defined inside System.Web) has the following
signature for the this accessor.

object this[int index] { get; set; }
object this[string name] { get; set; }

Am I setting the wrong expectation or is moq not supporting the object
this[string name] property signature?

Thanks

Phil


Daniel Cazzulino

unread,
May 1, 2008, 9:50:18 AM5/1/08
to moq...@googlegroups.com
we need to improve support for indexers.
for now, if you need to mock the getter for the indexer, just use Expect(...)
Typically that's what you want to mock, the getter rather than the setter...

philipp...@gmail.com

unread,
May 6, 2008, 2:27:14 PM5/6/08
to MoQ Discussions

I ended up creating a hand coded mock for my needs since I really
wanted to ensure that people would properly set some values inside the
session when a certain method is called.


Reply all
Reply to author
Forward
0 new messages