我一直在试图弄清楚我正在编写的这个特定测试发生了什么,现在再次伸出手来看看是否有人可能知道发生了什么。
我有一个 ModeltoXml 方法,当我直接通过另一个测试类进行测试时,它很好。然而,在另一种方法中,我正在测试调用 ModeltoXml 我在 Rhino Mock 中创建的存根始终返回 null。
下面大致是我的代码如何查找我通过构造函数传递我的存根方法的测试。
[Test]
var newEntry = new Model1{name="test"};
var referrer = "http://example.com";
var stubbedConfigMan = MockRepository.GenerateStub<IConfigurationManager>();
var supportMethods = MockRepository.GenerateStub<ISupportMethods>();
stubbedConfigMan.Stub(x => x.GetAppSetting("confEntry1")).Return("pathhere");
supportMethods.Stub(x => x.ModeltoXml(newEntry)).IgnoreArguments().Return("test"); //this is the line of code which should be setting the return value
var testObject = new MyApi().NewTicket(newEntry, referrer, supportMethods, stubbedConfigMan);
为此测试调用的具体方法有如下一行:
public MyResponseModel NewTicket(Model1 newTicket, string referrer, ISupportMethods supportMethods,IConfigurationManager configMan)
{
//.. other code here
var getXml = supportMethods.ModeltoXml(newTicket); //This line always returns null
}
ModeltoXml 方法的一个简单入口:
public string ModeltoXml<T>(T item)
{
//Code here to serialize response
return textWriter.ToString();
}
我的界面上有什么:
public interface ISupportMethods
{
string ModeltoXml<T>(T item);
}
我创建了另一种基本方法,它将除外并返回一个字符串值。我在同一个班级中得到了这个工作,但是在测试中我不得不在存根上使用 .IgnoreArguments 选项。此后在调试时我看到了设置的返回值。
但是,当我尝试在我的测试类中有问题的代码行上做同样的事情时,我仍然看到在调试期间我的测试类中没有出现返回值。
所以我有点卡住了!在我的测试中,我在其他存根上得到了返回值,而正是这种特殊的方法/存根不会发挥作用。
谢谢。
更新
我发现的一个更简单的例子是在相同的测试下:
var stubbedConfigMan = MockRepository.GenerateStub<IConfigurationManager>();
var supportMethods = MockRepository.GenerateStub<ISupportMethods>();
supportMethods.Stub(x => x.ModeltoXml(newEntry)).IgnoreArguments().Return("test").Repeat.Any();
//the class under test is successfully returning 'hero' when this method is called.
supportMethods.Stub(x => x.mytest(Arg<string>.Is.Anything)).Return("hero");
//If I use this line and debug test9 is set with my return value of "test".
//However in the same run my class under test the method just returns null.
var test9 = supportMethods.ModeltoXml(newEntry);
var testObject = new MyApi().NewTicket(newEntry, referrer, supportMethods, stubbedConfigMan);
上面的例子表明,将我的模拟/存根对象传递给我的测试类,ModeltoXml 存根会发生一些事情。我看不出/想不出发生这种情况的任何原因。