0

使用[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

4

1 回答 1

1

从你的类中删除“密封”(如果它不能从它继承,Rhino.Mocks 就不能代理你的类)。并且 ExecuteQueryProc 必须是虚拟的才能存根其功能。

于 2012-08-17T20:52:30.970 回答