I'm new to NSubstitute and trying to fake an existing class named OrgDataWS
. This class has a method named GetDataSet
:
public XmlElement GetDataSet(int token)
{
string perfLogMessage = string.Format("Org.GetDataSet: {0}", Guid.NewGuid().ToString());
MultiMessagePerformanceCounter performanceCounter = MultiMessagePerformanceCounter.StartNew(perfLogMessage);
XmlElement result = orgDataManager.GetDataSet(token);
performanceCounter.Stop();
return result;
}
The following is my test methods:
[TestMethod]
public void GetDataSetTest()
{
var dataWSStub = Substitute.For<OrgDataWS>();
var orgManagerStub = Substitute.For<OrgDataManager>();
var document = new XmlDocument();
var xmlElement = document.CreateElement("a");
orgManagerStub.GetDataSet(Arg.Any<int>()).Returns<XmlElement>(xmlElement);
dataWSStub.OrgDataManager = orgManagerStub;
var result = dataWSStub.GetDataSet(99);
}
However, when I run my test methods, this line
orgManagerStub.GetDataSet(Arg.Any<int>()).Returns<XmlElement>(xmlElement);
threw an exception. This exception is from the implementation of OrgDataManager
class, from my understanding, this is not supposed to happen. The purpose of using that clause is that I hope if the orgManagerStub
's DataDataSet
method is invoked with any Int
parameter, just return my xmlElement
instance. I didn't hope my code to run the detailed implementation of OrgDataManager
.
What's wrong with my test code? How to fix it?