【问题标题】:FakeItEasy CallsBaseMethod Nested Fake CallTo isn't called不调用 FakeItEasy CallsBaseMethod 嵌套的 Fake CallTo
【发布时间】:2013-06-05 21:21:52
【问题描述】:

我正在尝试混合 CallsBaseMethod 和 CallTo,但它没有调用我设置的那个。请参阅下面的代码和我的 cmets。 FakeItEasy 有没有办法让它发挥作用或有不同的方法?

public LayoutManager(ICompanyManager companyManager)
{
    this._companyManager = companyManager;
}

this.CompanyManagerFake = A.Fake<ICompanyManager>();
// using StructureMap, put this here to make the example more brief, in my code it's in a base class
 ObjectFactory.Configure(registry =>
 {
   registry.For<ICompanyManager>().Use(this.CompanyManagerFake);
});
this._layoutManager = A.Fake<LayoutManager>();
var layouts = GetTestLayouts();

// I want to get the actual GetLayoutForUser method
A.CallTo(() => this._layoutManager.GetLayoutForUser(A<int>.Ignored)).CallsBaseMethod();

// I want to mock the data for the GetAll method (which is called in GetLayoutForUser)
A.CallTo(() => this._layoutManager.GetAll(A<string>.Ignored)).Returns(layouts.AsQueryable());
A.CallTo(() => this.CompanyManagerFake.GetAll(A<string>.Ignored)).Invokes(
    call =>
    {
        // this doesn't get called from GetLayoutForUser, but is from the line below
        var x = call.Arguments;     
    });

// I want to use .Returns(new List<Company>().AsQueryable()); instead of Invokes, but needed to set a breakpoint
// this hits the above Invokes as expected
var assignedCompanIds = this.CompanyManagerFake.GetAll()
    .Where(c => c.UserProfiles.Any(up => up.UserId == 123)
            || c.UserProfiles1.Any(up => up.UserId == 123))
    .Select(c => c.CompanyId);

 // Act
 var result = this._layoutManager.GetLayoutForUser(123);

 // Assert
 // something

注意:我也把这个问题放在GitHub 这似乎类似于this question,但我不能把它放在一起。 所以当我打电话时

var assignedCompanyIds = this._companyManager.GetAll()
                .Where(c => c.IsAssigned)
                .Select(c => c.CompanyId).ToList();

我得到了这个例外: {X} 方法抛出异常: System.ArgumentException:“”类型的表达式不能用于“System.Linq.IQueryable1[Company]' of method 'System.Linq.IQueryable1[Company]”类型的参数 Where[Company](System.Linq.IQueryable1[Company], System.Linq.Expressions.Expression1[System.Func`2[公司,System.Boolean]])'

【问题讨论】:

  • 我目前也遇到这种情况。
  • @Aligned,我在关注这个问题时感觉很糟糕。你有一个更简单的例子来展示这个问题吗? (或者,1.23.0 仍然会发生这种情况吗?)
  • @BlairConrad 我在 GitHub 问题上添加了评论并关闭了它。我将无法找到时间来更好地解决这个问题。感谢您的关注。

标签: mocking fakeiteasy


【解决方案1】:

我通过标记我想要被称为公共而不是内部的方法来解决我的问题,因为显然 [assembly: InternalsVisibleTo("TestProject")] 没有正常工作。

【讨论】:

  • 有趣。所以你把它们公开了,这样你就可以用 FakeItEasy (A.CallTo(() => ...) 来模拟它们?
  • 是的!我删除了 InternalsVisibleTo 并将方法公开而不是内部,它起作用了。一旦我重新构建、清理并重新添加了可见的内部结构,并将其重新设置为内部结构,它就可以再次工作了。
  • 就我而言,这些功能已经公开。感谢您的回答,也许它会帮助别人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-28
相关资源
最近更新 更多