2013-06-05 30 views
1

我想混合CallsBaseMethod和CallTo,它不會調用我設置的那個。請參閱下面的代碼和我的意見。有沒有辦法讓這個工作或與FakeItEasy不同的方法?FakeItEasy CallsBaseMethod嵌套假CallTo不叫

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 [公司] [公司] [公司](System.Linq.IQueryable 1[Company], System.Linq.Expressions.Expression 1 [System.Func`2 [Company,System.Boolean]])'

+0

我目前也遇到過這種情況。 –

+0

@對齊,我在這個問題後有一段可怕的時間。你有一個更簡單的例子,表現出這個問題? (或者,它是否仍然發生在1.23.0?) –

+0

@BlairConrad我在GitHub問題上添加了一條評論並關閉了它。我無法找到時間將這種情況變得更好。感謝您的關注。 – Aligned

回答

0

我解決了我的問題,標記我的方法我想被稱爲public的內部,因爲顯然[assembly:InternalsVisibleTo(「TestProject」)]工作不正常。

+0

有趣。所以你讓它們公開,所以你可以用FakeItEasy(A.CallTo(()=> ...)來嘲笑它們) – Aligned

+0

是的!我刪除了InternalsVisibleTo並且公開了這個方法,而不是內部的,它起作用了。 ,清理並重新添加內部可見部分,然後重新將其內部化,然後再次運行。 –

+0

在我的情況下,這些功能已經公開了。感謝您的回答,也許它會幫助別人。 – Aligned