6

我的控制器中有这样的操作方法

public ActionResult Index()
{
    using (NorthwindDataContext db = new NorthwindDatacontext())
    {
        var results = db.GetRecordSets(arg1, ....).ToList();
        // use results as list
    }

    return View();
}

我想开始对其进行测试(是的,在它建成之后,而不是之前......但是代码是在我开始使用 TDD 之前编写的,所以......)

我发现将诸如此类的属性添加到控制器

public delegate NorthwindDatacontext ContextBuilderDelegate();

public ContextBuilderDelegate ContextBuilder { get; set; }

我可以在构造函数中添加这样的东西......

ContextBuilder = () => new NorthwindDatacontext();

然后我可以使用 NorthwindDatacontext 的模拟来测试设置 ContextBuilder 属性的 ActionMethod

var controller              = new MyController();
var mockDataContext         = new Mock<NorthwindDatacontext>();
controller.ContextBuilder   = () => mockDataContext.Object;

但是......我发现没有办法使用它,因为 NorthwindDatacontext 的所有方法都使用 ISingleResult 作为 returnType 并且我找不到使用该接口创建对象的方法。我试过这个

var theResult                  = new List<GetRecordSetsResult>();
// fill the data structure here with the provided result...

mockDataContext.Setup(c => c. GetRecordSets()).Returns(theResult as
                                            ISingleResult<GetRecordSetsResult>);

但它不起作用,因为转换为 ISingleResult 时 theResult 为空。

有没有办法创建一个 ISingleResult 对象来测试这种方式,或者我在这里做的事情不正确?

提前致谢

4

2 回答 2

5

ToList()是IEnumerable的扩展方法,很容易mock,因为它只有一个成员方法——GetEnumerator() .

如果它的方法不是虚拟的,你仍然可能在模拟 NorthwindDataContext 类时遇到问题......

无论如何,这就是我在沙盒中解决类似问题的方法,希望对您有所帮助:

public class MyType
{
    public virtual ISingleResult<int> ReturnSomeResult() { throw new NotImplementedException(); }
}

[TestMethod]
public void TestMethod1()
{
    var mockMyType = new Mock<MyType>();
    var mockSingleResult = new Mock<ISingleResult<int>>();
    IEnumerable<int> someEnumerable = new int[] {1,2,3,4,5};
    mockSingleResult.Setup(result => result.GetEnumerator()).Returns(someEnumerable.GetEnumerator());
    mockMyType.Setup(myType => myType.ReturnSomeResult()).Returns(mockSingleResult.Object);

    Assert.AreEqual(15, mockMyType.Object.ReturnSomeResult().ToList().Sum());
}
于 2009-05-19T16:37:13.000 回答
5

我创建了一个实现 ISingleResult 的类,并在其中放入了一个 List。我对这种类型的编码相当陌生,所以虽然这对我有用,但使用风险自负(如果你看到漏洞发表评论)。

class SingleResult<T>:ISingleResult<T>
{
    readonly List<T> _list = new List<T>();

    public void Add(T item)
    {
        _list.Add(item);
    }

    #region Interface Items

    public IEnumerator<T> GetEnumerator()
    {
        return _list.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public object ReturnValue { get { return _list; } }

    public void Dispose() { }

    #endregion
}

然后可以使用它来返回模拟的一部分。这就是我最终将它与 Rhino Mocks 一起使用的方式:

[TestMethod]
public void TestSomething()
{
    //Arrange
    // Make a data context and DAL
    var _ctx = MockRepository.GenerateMock<IDataClassesDataContext>();
    var someDALClass = new SomeDALClass(_ctx);

    User testUser = UserObjectHelper.TestUser();
    SingleResult<User> userList = new SingleResult<User> { testUser };

    // Indicate that we expect a call the to sproc GetUserByUserID
    _ctx.Expect(x => x.GetUserByUserID(testUser.UserID)).Return(userList);

    //Act
    someDALClass.UpdateUser(testUser);

    //Assert
    Assert.IsTrue(SomeTestCondition());
}
于 2009-12-07T20:10:57.797 回答