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
C# and NaturalMocks; I would appreciate some help with this.
Trying to test this simple property on a derived class

public override string Name
 {
     protected set
     {
         if (DmReplicatedThings.Count > 0)
         {
             DmReplicatedThings[0].Name = value;
         }
         //else
         //   Should never happen; just ignore

         base.Name = value;
     }
 }


1)
DmReplicatedThings is a private property method that returns a collection.
When the code tries to retrieve this, the test code attempts to provide a real collection containing real objects.

  using (RecordExpectations expect = RecorderManager.StartRecording())
 {
         BlahBlahBlahCollection things= accessor.DmReplicatedThings;
         expect.Return(My_DmReplicatedThings);

         things[0].Name = val;
 }


expect.Return results in this error
*** Cannot use Return in this sequence, there must be a mocked statement first
Perhaps you are trying to mock a method from mscorlib.

In fact, I see this error quite frequently and I typically do not understand what the problem is.
I've learned one thing that can cause this error (but it not the case here) in which the error text is entirely misleading/inaccurate.


2) Using a mock & RecordExpectations, I have no idea how to verify this line
base.Name = value;
asked by cabin (2.3k points)

3 Answers

0 votes
Hi

Are you using MSTest accessors?
the isolator supports MSTest accessors but not the others (MBUnit for examples).

If you are using MSTest accessors please post the full test code and mark the line where you get the exception.

To verify setting a property use MockManager.Verify at the and of the test
or use VerifyMocks attribute at the class or method level.

About the error message:
The Isolator is expecting a mocked method call before the call to RecordExpectations .Return method.
The Isolator can not mock mscorlib types so when you it sees such a call inside the recording block it just skips it hence the part "Perhaps you are trying to mock a method from mscorlib."

Having said that, I agree that the message is not clear enough - We will try to make it clearer.

:idea: Did you try to use the new Arange Act Assert API?
It can make the test easier to understand. Although the AAA API does not support private accessors you can fake private properties and methods with it.
answered by ohad (35.4k points)
0 votes
Yes, MSTest accessors
However, the accessor was built upon a mocked object rather than a real object. This apparently was the root of my problem. Somehow, I thought that I could test my class without creating a real instance.

Regarding the new AAA api, it has been said that this requires .Net framework 3.5, yes? That is not an option. Our product is standardized on 2.0. Cannot simply change the framework and pray that millions of lines of code will be unaffected.
answered by cabin (2.3k points)
0 votes
Hi Cabin

I'm not clear about the state of the test right now :shock:
Did you find what's the problem? If you still need help try to post the full test code and I'll try to help you with it.

About the AAA API:
You can use with VS2005 and .NET framework 3.5 installed on the test machine. You can look at this post in our blog.
Note it's just a suggestion. We know that its not always possible, that's why we keep the previous API.
answered by ohad (35.4k points)
...