Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Rhino mocks资产调用不起作用_C#_Unit Testing_Rhino Mocks_Stub_Rhino Mocks 3.5 - Fatal编程技术网

C# Rhino mocks资产调用不起作用

C# Rhino mocks资产调用不起作用,c#,unit-testing,rhino-mocks,stub,rhino-mocks-3.5,C#,Unit Testing,Rhino Mocks,Stub,Rhino Mocks 3.5,我编写了以下测试用例,以使用Rhino Mock说明我的问题: [TestClass] public class Tester { public class TestList<T> : List<T> { public override bool Equals(object obj) { // obj is an empty list here?? return base.Eq

我编写了以下测试用例,以使用Rhino Mock说明我的问题:

[TestClass]
public class Tester
{
    public class TestList<T> : List<T>
    {
        public override bool Equals(object obj)
        {
            // obj is an empty list here??
            return base.Equals(obj);
        }
    }

    public interface IStubbedInterface
    {
        void DoSomethingWithList(TestList<int> list);
    }

    public class ClassToTest
    {
        public IStubbedInterface TheStub { get; set; }

        public void Run()
        {
            var list = new TestList<int> { 1, 2 };
            TheStub.DoSomethingWithList(list);
            list.Clear();
        }
    }

    public bool Match(TestList<int> arg)
    {
        // Here arg is an empty list??
        return arg == new TestList<int> { 1, 2 };
    }

    [TestMethod]
    public void Test()
    {
        var classToTest = new ClassToTest();
        classToTest.TheStub = MockRepository.GenerateMock<IStubbedInterface>();
        classToTest.Run();
        classToTest.TheStub.AssertWasCalled(
            x => x.DoSomethingWithList(new TestList<int>() { 1, 2 }));
            //x => x.DoSomethingWithList(Arg<TestList<int>>.Matches(arg => Match(arg))));
    }
}
[TestClass]
公共类测试员
{
公共类TestList:List
{
公共覆盖布尔等于(对象对象对象)
{
//obj在这里是一个空列表??
返回基数等于(obj);
}
}
公共接口是SubbedInterface
{
void DoSomethingWithList(测试列表);
}
公共类类测试
{
公共ISsubbed接口{get;set;}
公开募捐
{
var list=newtestlist{1,2};
具有列表(列表)的容器dosomething;
list.Clear();
}
}
公共布尔匹配(TestList arg)
{
//这里arg是一个空列表??
返回arg==newtestlist{1,2};
}
[测试方法]
公开无效测试()
{
var classToTest=新的classToTest();
classToTest.TheStub=MockRepository.GenerateMock();
classToTest.Run();
调用classToTest.TheStub.Asserts(
x=>x.DoSomethingWithList(newTestList(){1,2}));
//x=>x.DoSomethingWithList(Arg.Matches(Arg=>Match(Arg));
}
}
不管我是直接比较列表还是使用Arg.Matches(..)语法,测试用例都会在AssertWasCalled()行失败。我还尝试使用MockRepository.GenerateSub(..)而不是GenerateMock(..),这也失败了。由于list.Clear()的原因,它失败;调用DoSomethingWithList()后的行,这将导致在调用AssertWasCalled()时列表也为空。这是一只模仿犀牛的虫子吗?我假设RhinoMocks在调用断言函数时会以某种方式记录参数的快照,但RhinoMocks似乎一直在使用同一个对象


在我遇到这个问题的实际案例中,我正在测试的函数的参数被包装在一个using()语句中,这导致了AssertWasCall无法测试传递的参数的相同效果。

看起来使用Expect()/verifyAllExpections()模式更适合此测试场景。将测试用例更改为以下代码将通过

这是相当不幸的,因为我更喜欢使用AssertWasCalled(),但目前它相当不可靠

[TestMethod]
public void Test()
{
    var classToTest = new ClassToTest();
    classToTest.TheStub = MockRepository.GenerateMock<IStubbedInterface>();
    classToTest.TheStub.Expect(
        x => x.DoSomethingWithList(new TestList<int>() { 1, 2 }));
    classToTest.Run();
    classToTest.TheStub.VerifyAllExpectations();
}
[TestMethod]
公开无效测试()
{
var classToTest=新的classToTest();
classToTest.TheStub=MockRepository.GenerateMock();
classToTest.TheStub.Expect(
x=>x.DoSomethingWithList(newTestList(){1,2}));
classToTest.Run();
classToTest.TheStub.VerifyAllExpections();
}