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
Hello,
I have 1 issue with type data set when I pass to method typed datarow value and try to mock such a call. e.g.:

Isolate.WhenCalled( () => someObj.Do(typedDataRow.FooTypedProperty)).IgnoreCall();


FooTypedProperty property has been mocked as well as Do method call and returns null when I try to read its value.

I was able to reproduce issue (or may be this is expected behavior?) in example below:
1. Classes definition:
public class FooClass
{
    public void Do(string input)
    {
        //do some task
    }
}

public class SubFooClass
{
    public string Value { get; set; }
}


2. Tests:
[TestMethod]
[Isolated]
public void IsItValidMockTest()
{
    //expectations
    string expected = "faked value";
    //prepare
    FooClass foo = new FooClass();
    SubFooClass subFoo = new SubFooClass() { Value = expected };
    Isolate.WhenCalled(() => foo.Do(subFoo.Value)).IgnoreCall();
    //run
    foo.Do(subFoo.Value);
    //assertations
    Assert.AreEqual(expected, subFoo.Value); //assertation fails. value is null
    Isolate.Verify.WasCalledWithExactArguments(() => foo.Do(subFoo.Value)); //verification fails. method wasn't called
}


More over when I try to make sure that Do is called with exact arguments I get the following error:
"*** Cannot mock types from mscorlib assembly."

[TestMethod]
[Isolated]
public void IsItValidMockTestAnother()
{
    //expectations
    string expected = "faked value";
    //prepare
    FooClass foo = new FooClass();
    SubFooClass subFoo = new SubFooClass() { Value = expected };
    Isolate.WhenCalled(() => foo.Do(subFoo.Value)).WithExactArguments().IgnoreCall(); //fails with "*** Cannot mock types from mscorlib assembly."
    //run
    foo.Do(subFoo.Value);
    //assertations
    Assert.AreEqual(expected, subFoo.Value);
    Isolate.Verify.WasCalledWithExactArguments(() => foo.Do(subFoo.Value));
}


Is it expected behavior? If yes then how should I mock such a method call?

TIA,
Grammer
asked by grammer (1.8k points)

2 Answers

0 votes
Hi,

This issue may happen due to nested calls inside WhenCalled and Verify. The next version of Isolator supports identification of nested calls.

Nested calls are usage of method or property inside arguments list. The solution is to store the value of the property call before calling WhenCalled.
var prop = typedDataRow.FooTypedProperty;

Isolate.WhenCalled( () => someObj.Do(prop)).IgnoreCall();


Just to make clear, if you're using WhenCalled without appending WithExactArguments the behavior will relate to any argument used.

These calls will set the same behavior on Do()
var prop = typedDataRow.FooTypedProperty;
Isolate.WhenCalled( () => someObj.Do(prop)).IgnoreCall();

Isolate.WhenCalled( () => someObj.Do(null)).IgnoreCall();


Regards,
Elisha
Typemock Support
answered by Elisha (12k points)
0 votes
Thanks for your quick answer.

Hi,

This issue may happen due to nested calls inside WhenCalled and Verify. The next version of Isolator supports identification of nested calls.

Nested calls are usage of method or property inside arguments list. The solution is to store the value of the property call before calling WhenCalled.
var prop = typedDataRow.FooTypedProperty;

Isolate.WhenCalled( () => someObj.Do(prop)).IgnoreCall();




Thank you! That will be great. Looking forward to new version. Workaround works for me as well.

Just to make clear, if you're using WhenCalled without appending WithExactArguments the behavior will relate to any argument used.

These calls will set the same behavior on Do()
var prop = typedDataRow.FooTypedProperty;
Isolate.WhenCalled( () => someObj.Do(prop)).IgnoreCall();

Isolate.WhenCalled( () => someObj.Do(null)).IgnoreCall();




I got it and I just want to say about another issue when WithExactArguments is specified together with nested property call (see my example in 1st post).

Thanks again,
Grammer
answered by grammer (1.8k points)
...