17
(如標籤所示,我正在使用moq)。如何模擬只讀屬性,其值取決於模擬的另一個屬性
我有一個這樣的接口:作爲源
interface ISource
{
string Name { get; set; }
int Id { get; set; }
}
interface IExample
{
string Name { get; }
ISource Source { get; set; }
}
在我的應用程序,的IExample的具體實例接受DTO(IDataTransferObject)。有關IExample的具體實現的一些屬性只是委派給Source。像這樣...
class Example : IExample
{
IDataTransferObject Source { get; set; }
string Name { get { return _data.Name; } }
}
我想創建的IExample的一個獨立的模擬(獨立的意思,我不能使用捕獲變量,因爲模擬的IExample的幾個實例將在測試的過程中創建)和設置模擬,使IExample.Name返回IExample.Source.Name的值。所以,我想創建一個模擬的東西是這樣的:
var example = new Mock<IExample>();
example.SetupProperty(ex => ex.Source);
example.SetupGet(ex => ex.Name).Returns(what can I put here to return ex.Source.Name);
從本質上講,我想配置的模擬返回,作爲一個屬性,模擬的子對象的屬性值的值。
謝謝。
這正是我想要的。我從來沒有想過要捕捉我想參考的Mock的局部變量來獲取底層的價值。這會讓我的大腦受傷。 – wageoghe 2012-07-18 16:37:12