【问题标题】:FakeItEasy return object that the method is called withFakeItEasy 返回调用该方法的对象
【发布时间】:2017-09-26 10:23:27
【问题描述】:

我想这样设置我的假货:

A.CallTo(() => this.repository.Create(A<PersonModel>._)).Returns(XYZ);

其中XYZ 与插入到A&lt;PersonModel&gt;._ 的变量相同

所以如果CreatemySamplePersonModel 调用,我希望该方法返回mySamplePersonModel

我怎样才能做到这一点?

提前致谢

【问题讨论】:

    标签: c# .net unit-testing mocking fakeiteasy


    【解决方案1】:

    您找到的解决方案是正确的。有一个更易读的 IMO 替代方案:

    A.CallTo(() => repository.Create(A<PersonModel>._)).ReturnsLazily((PersonModel p) => p);
    

    【讨论】:

      【解决方案2】:

      我找到了可以像这样捕获参数的答案:

      A.CallTo(() => this.repository.Create(A<PersonModel>._)).ReturnsLazily(x => x.Arguments.Get<PersonModel>(0));
      

      你甚至可以像这样修改这个参数:

      A.CallTo(() => this.repository.Create(A<PersonModel>._)).ReturnsLazily(x =>
                  {
                      var personModel = x.Arguments.Get<PersonModel>(0);
                      personModel.Name = "aName";
                      return personModel;
                  });
      

      如果有人有更优雅的解决方案,请随时发布:-)

      【讨论】:

        猜你喜欢
        • 2015-02-11
        • 1970-01-01
        • 1970-01-01
        • 2013-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多