[英]Moq - how to access a class member when mocking?
假設這個課程:
public class Foo
{
public string Bar { get; set;}
Foo()
{
this.Bar = "Hello world";
}
public void DoStuff()
{
this.Bar = "BAR" // imagine this is read from a memory stream
}
}
我想模擬Foo並進行設置,以便可以在DoStuff()中介紹自己的行為-即對Bar成員進行一些操作。 但是,如何從DoStuff()的回調中訪問Bar?
我試過的:-DoStuff()上的回調似乎無法訪問類狀態-我可以設置Bar getter,但這太籠統,因為其他操作也讀取Bar
您不會因為自己在做什么而嘲笑Foo
。 模仿用於在特定實例內提供嚴格控制的依賴關系。
例如,如果Foo
具有在構造函數中傳遞的IBaz
類型的成員,則可以創建IBaz
的模擬,在其中告訴IBaz
當Foo
對其接口進行調用以觸發Foo
本身的行為時如何反應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.