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
I am trying to replace a method call with a string... here is my example:

DataAccess dataAccess = new DataAccess();
XmlDocument entityXml = new XmlDocument();
entityXml.LoadXml(dataAccess.ExecStoredProc("DC_SP_PLT_GetEntityAsXml", entityId, includeChildren, includeParents, showAdvancedDetails).Replace("&", "&"));
Entity entity = new Entity(entityXml.DocumentElement);
return entity;

I am trying to replace dataAccess.ExecStoreProc(...) with a string... so the entityXml.LoadXml will work..

I can't change any of the above code..not sure how to go about doing this...
Thanks!
asked by leeroy25 (640 points)

3 Answers

0 votes
Hi and welcome to the forum :)

Here's how to fake the string returned from dataAccess.ExecStoredProc:
[Test]
public void Test()
{
    DataAccess fake = Isolate.Fake.Instance<DataAccess>();
    Isolate.WhenCalled(() => fake.ExecStoredProc(null, null, false, false, false)).WillReturn("My faked string");
    Isolate.Swap.NextInstance<DataAccess>().With(fake);
    // call the code under test here ...
}


Few key notes:
1. the first line just create a fake instance of DataAccess with default behavior of recursive fakes. That means that each void method will be ignored and each method that returns something will return its default value for primitives and fake objects for reference types.
2. In the second line you set the faked string you want to return from the method ExecStoredProc. In the example it will return the string "My faked string"
3. In the third line we replace the the next instance of DataAceess with our fake. This means next time "new DataAccess()" gets called it will return a faked instance with the faked behavior you have set before.

Please let me know if it helps.
answered by ohad (35.4k points)
0 votes
Thanks for the quick reply.. That looks great.. only problem is that "My Fake String" is a variable and changes on every assert... for example:

DataAccess fake = Isolate.Fake.Instance<DataAccess>();
Isolate.WhenCalled(() => fake.ExecStoredProc(null, null, false, false, false)).WillReturn("MyVariable");
Isolate.Swap.NextInstance<DataAccess>().With(fake);

MyVariable = SomeString;
**perform some action**
**assert**

Myvariable = AnotherString;
**perform some action**
**assert**

I got my test to pass using:
Isolate.WhenCalled(() => DBEntities.GetEntity(entityID, includeChildren, includeParents)).DoInstead(Context =>
{
DataAccess dataAccess = new DataAccess();
XmlDocument entityXml = new XmlDocument();
entityXml.LoadXml(xmlDoc);
Entity entity = new Entity(entityXml.DocumentElement);
return entity;
});

but with any bigger methods or classes.. that will get old quick...


Thanks!
answered by leeroy25 (640 points)
0 votes
Hi,

Not sure understand since you refer to "MyVariable" as constant string and like a variable.
Here is my best shot :)

You can use sequenced fakes, that means that when you use WillReturn more that once the first time the method will be called it will return the first value and the second the second value you set and so on.
Example:
[Test]
public void Test()
{
    DataAccess fake = Isolate.Fake.Instance<DataAccess>();
    Isolate.WhenCalled(() => fake.ExecStoredProc(null, null, false, false, false)).WillReturn("My faked string");
    Isolate.WhenCalled(() => fake.ExecStoredProc(null, null, false, false, false)).WillReturn("My other faked string");

    Assert.AreEqual("My faked string", fake.ExecStoredProc(null, null, false, false, false));
    Assert.AreEqual("My other faked string", fake.ExecStoredProc(null,null, false, false, false));            
} 


Hope it helps.
answered by ohad (35.4k points)
...