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
0 votes
Hi,

I am trying to mock a call to a private method on an instance of a class so I can control it's return value. I am using VS2005, so using private accessors and keep getting the same error:

Cannot use Return in this sequence, there must be a mocked statement first

My code is:

EditorFacade target = new EditorFacade();
EditorFacadeAccessor accessor = new EditorFacadeAccessor(target);

string[] assemblyNames = new string[] { "John" };

using (RecordExpectations recorder = RecorderManager.StartRecording())
{
accessor.GetAssemblyNames();
recorder.Return(assemblyNames);
}

It is the call to Return that throws the exception. I have tried a few variations on the theme, but simply cannot get it working.

I am using 3.7.1 on Vista.

Thanks

Colin
asked by desmondc (760 points)

9 Answers

0 votes
Colin,
This should work, I have just run a test on my machine and it works.
Please post the EditorFacadeAccessor.GetAssemblyNames() signature so that I can try to reproduce the bug.
answered by scott (32k points)
0 votes
Thanks for looking at this Scott, the signature for the method is:

private string[] GetAssemblyNames()

which is why I was trying to return an array of strings.

Is there anyway to achieve this with reflective mocks?

Colin
answered by desmondc (760 points)
0 votes
Strange,
I have tried it and it works.
Please run with Log enabled (TypeMock Configuration->Visual Studio->Log Enabled) and send the log (I will send you the address via e-mail).
answered by scott (32k points)
0 votes
I have also tried using reflective mocks, which also fails.

string[] assemblyNames = new string[] { "John" };

MockManager.Init();
Mock mockFacacde = MockManager.Mock(typeof(EditorFacade));
mockFacacde.ExpectAndReturn("GetAssemblyNames", assemblyNames, 1);

EditorFacade target = new EditorFacade();
EditorFacadeAccessor accessor = new EditorFacadeAccessor(target);

Type attributeType = typeof(Commands.MessageCommandAttribute);

System.Collections.Generic.Dictionary<System.Type, System.Type> table = new Dictionary<Type, Type>();

accessor.IndexAttributedClasses(attributeType, table);

MockManager.Verify();

Index AttributedClasses calls GetAssemblyNames, so I'd expect Verify to be happy with one call, but it fails saying the method hasn't been called.

Colin
answered by desmondc (760 points)
0 votes
Hi,
Could you check to see if the file "c:\Program Files\TypeMock\TypeMock.NET\namespaces.dat" exists and is readable.

Also please run RegEdit.exe and post the value of HKEY_CLASSES_ROOT\CLSID\{E1A6A8C8-C7FA-4F97-843F-83F638EA4248}\InprocServer32 (default)
answered by scott (32k points)
0 votes
Yes the namespaces.dat exists, is read & writeble and contains the following text:

nunit
vstesthost.exe
MbUnit
TestDriven.TestRunner.dll
JetBrains.ReSharper.UnitTestRunner.exe


The reg key has the following subkeys

(Default) REG_SZ C:\Program Files\TypeMock\TypeMock.NET\MockWeaver.dll

ThreadingModel REG_SZ Both

Colin
answered by desmondc (760 points)
0 votes
Open TypeMock Configuration->Visual Studio Tab

Make sure that Fast Mode is NOT selected.
Also make sure that you don't have an environment variable TMOCK_FAST set.
answered by scott (32k points)
0 votes
Scott, you are a hero!

I must have been fiddling with the settings yesterday and ticked that box is desperation. Now it is unticked, both the natural and reflective mocks work!

Suggestion: make it very clear in the config tool what that tick box does and when you might want to use it?

Thanks again!

Colin
answered by desmondc (760 points)
0 votes
Your advice Colin is exactly what we want to do.
We will create a better message for the tick box
answered by scott (32k points)
...