FakeItEasy CallsBaseMethod嵌套的假CallTo未被调用

时间:2013-06-05 21:21:52

标签: mocking fakeiteasy

我正在尝试混合使用CallsBaseMethod和CallTo,但它并没有调用我设置的那个。请参阅下面的代码和我的评论。有没有办法让这个工作或与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.IQueryable 1[Company]' of method 'System.Linq.IQueryable类型的参数1 [公司]其中[公司](System.Linq.IQueryable 1[Company], System.Linq.Expressions.Expression 1 [系统。 Func`2 [公司,System.Boolean]])'

1 个答案:

答案 0 :(得分:0)

我通过标记我想要被称为public而不是internal的方法修复了我的问题,因为显然[assembly:InternalsVisibleTo(“TestProject”)]无法正常工作。