访问装配在返回值时,期望()的原始参数() [英] Accessing the original arguments of Expect() when assembling the value in Returns()

查看:81
本文介绍了访问装配在返回值时,期望()的原始参数()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能获得访问用于组装返回对象时拨打电话到嘲笑的期望参数?



下面是涉及到的对象存根并且,鉴于此,我试图嘲弄集合:

 类CollectionValue {
公共ID {搞定;组; }
}
类集合{
私有列表< CollectionValue> AllValues {搞定;组; }
公开名单< CollectionValue> GetById(列表< INT>的id){
返回AllValues.Where(V => ids.Contains(v.Id));
}
}



鉴于CollectionValues的测试列表将用于嘲笑的对象,一个人如何去建立一个期望,会处理CollectionValues的该列表中ID的每一个可能的排列,包括结合现有的ID和不存在的ID的电话?我的问题来自于建立在单一调用所有可能期望的愿望;如果访问原始参数是不可能的,我可以很容易地建立只是确切的期望我想在一个给定的通话每次测试。



下面是我希望做的,其中???代表哪里会方便有访问用于调用GetById(即合格It.IsAny限制的)参数:

  CollectionMock.Expect(C => c.GetById(It.IsAny<名单,LT; INT>>()))。返回(???); 


解决方案

从起订量的quickstart 指南:

  //访问调用参数返回时的值
mock.Setup(X => x.Execute(It.IsAny<串GT;()))
.Returns((字符串s)=> s.ToLower( ));



因此这意味着你可以在你填写?为



  CollectionMock.Expect(C => c.GetById(It.IsAny<名单< INT>>()) )
.Returns((名单< INT→1)=> //做一些东西以L
);


Is it possible to get access to the parameter used to make a call to a mocked expectation when assembling the Returns object?

Here is a stub for the objects involved and, given that, I am trying to mock a Collection:

Class CollectionValue {
    public Id { get; set; }
}
Class Collection {
    private List<CollectionValue> AllValues { get; set; }
    public List<CollectionValue> GetById(List<int> ids) {
        return AllValues.Where(v => ids.Contains(v.Id));
    }
}

Given a test list of CollectionValues that will be used for the mocked object, how does one go about setting up an expectation that will handle every possible permutation of the IDs in that list of CollectionValues, including calls that combine existing IDs and non-existing IDs? My problem comes from a desire to set up all possible expectations in a single call; if access to the original parameter isn't possible, I could just as easily set up just the exact expectation I want to test in a given call each time.

Here is what I was hoping to do, where "???" represents where it would be handy to have access to the parameter used to call GetById (the one that qualified the It.IsAny restriction):

CollectionMock.Expect(c => c.GetById(It.IsAny<List<int>>())).Returns(???);

解决方案

From the moq quickstart guide:

// access invocation arguments when returning a value
mock.Setup(x => x.Execute(It.IsAny<string>()))
                .Returns((string s) => s.ToLower());

Which suggests therefore that you can fill in your ??? as

CollectionMock.Expect(c => c.GetById(It.IsAny<List<int>>()))
              .Returns((List<int> l) => //Do some stuff with l
                      );

这篇关于访问装配在返回值时,期望()的原始参数()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆