Getting the value passed into an Indexer

53 views
Skip to first unread message

Griff

unread,
Jun 13, 2012, 3:09:53 AM6/13/12
to Rhino.Mocks
Hi everyone

I need to find out the value passed into an indexer.

My code (c#) that I need to test is as follows:

string cacheKey = GetCacheKey(cacheKeyRequest);
string cachedValue = myCache[cacheKey] as string;

So, I need to be able to identify the valle "cacheKey" passed into the
indexer.

I have attempted to achieve this using a Mock of the cache object:

var cache = MockRepository.GenerateMock<WebDataCache>();

The idea being that after the code was run, I would try:

var actualCacheKey = cache.GetArgumentsForCallsMadeOn(a =>
a["somevalue"], opt => opt.IgnoreArguments())[0][0].ToString();

This gives me a compilation error: Only assignment, call, increment,
decrement, and new object expressions can be used as a statement.

I saw one suggestion to make this a function in the following way:

var actualCacheKey = cache.GetArgumentsForCallsMadeOn(a =>
a["somevalue"] = null, opt => opt.IgnoreArguments())[0][0].ToString();

This now compiles,but throws a run-time InvalidOperationException: No
expectations were setup to be verified, ensure that the method call in
the action is a virtual (C#) / overridable (VB.Net) method call.

Any suggestions? [Am using RhinoMocks.3.6.1]

Many thanks in advance

Griff








Griff

unread,
Jun 13, 2012, 6:06:50 AM6/13/12
to rhino...@googlegroups.com
Related to this post, in another test I would want to stub the cache object and get it to return a particular value when the indexer is called, for example:

            string cachedObject = "<xml>";

            var cache = MockRepository.GenerateStub<WebDataCache>();

            cache.Stub(c => c["somedata"]).IgnoreArguments().Return(cachedObject);

This however throws an InvalidOperationException: Invalid call, the last call has been used or no call has been made (make sure that you are calling a virtual (C#) / Overridable (VB) method).

thanks....

Griff
Message has been deleted

Griff

unread,
Jun 13, 2012, 8:31:15 AM6/13/12
to rhino...@googlegroups.com
Solved the second part - how to program the Stub to return data:

            var cache = MockRepository.GenerateStub<IDataCache>();
            cache[expectedCacheKey] = cachedObject;

However, still not solved the main point of this post - to identify the cache key passed into the Indexer.

Griff

unread,
Jun 15, 2012, 11:42:37 AM6/15/12
to rhino...@googlegroups.com
Because of the lack of views to this post, I posted on StackOverflow:  http://stackoverflow.com/questions/11037639/rhinomocks-getting-the-value-passed-into-an-indexer-this

jimmy_keen was able to provide an answer:

The exception tells you exactly what is happening:

InvalidOperationException: No expectations were setup to be verified, ensure that the method call in the action is a virtual (C#) / overridable (VB.Net) method call.

Which means, in order for Rhino to properly work (or, in order for Castle to generate working proxies) your indexer has to be virtual. If you can't make it so, Rhino won't help you in this situation.

Once you make your indexer virtual, it is simple task:

var cache = MockRepository.GenerateMock<WebDataChache>();
cache.Expect(c => c["SomeKey"]).Returns("SomeValue");

// perform actual test

mock.VerifyAllExpectations();

This ensures that cache is accessed with ["SomeKey"]. If key value will be different, test will fail atVerifyAllExpectations line.

Reply all
Reply to author
Forward
0 new messages