如何对在密封的非泛型类中调用泛型方法的非泛型方法进行单元测试?

时间:2012-08-17 19:35:51

标签: unit-testing rhino-mocks

使用[assembly:InternalsVisibleTo()]

public sealed class AppService
{
    //TODO: fix dev's spelling
    //want to test this
    public AddSubscribtionResponse AddSubscribtion(AddSubscribtionRequest request)
    {
        return ExecuteQueryProc<AddSubscribtionResponse>("spAddAppToUserGroup", request).First();
    }

    //I want to stub or interaction test the call to here
    #if DEBUG
    internal
    #endif
    List<T> ExecuteQueryProc<T>(string query, object parameters = null)
    {
        var cn=GetConnection();
        //DatabaseCommand is a static class =(
        return DatabaseCommand.ExecuteQueryProc<T>(cn, query, parameters);
    }
}

我在使用Rhino进行存根或拦截Execute Query proc方法时所做的每一次尝试都失败了。

var service = MockRepository.GeneratePartialMock<AppService>(null);
service.Stub(f => f.ExecuteQueryProc<AddSubscribtionRequest>(null, null)).IgnoreArguments().Return(new List<AddSubscribtionRequest>());

var expected = new AddSubscribtionRequest();
var actual = service.AddSubscribtion(expected);

抛出 System.InvalidOperationException: ExecuteReader: CommandText property has not been initialized

1 个答案:

答案 0 :(得分:1)

取下&#34;密封&#34;来自你的班级(Rhino.Mocks如果不能继承它,就不能代理你的班级)。并且ExecuteQueryProc必须是虚拟的才能保留其功能。