【发布时间】: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