2011-04-27 27 views
8

錯誤:犀牛製品問題與私人二傳手存根

You are trying to set an expectation on a property that was defined to use PropertyBehavior. Instead of writing code such as this: mockObject.Stub(x => x.SomeProperty).Return(42); You can use the property directly to achieve the same result: mockObject.SomeProperty = 42;

var x = MockRepository.GenerateStub<MyClass>(); 
x.Stub(s => s.Items).Return(new List<Item>()); 

public class MyClass 
{ 
    public virtual IEnumerable<Item> Items 
    { 
     get {return _items;} 
     private set {_items = value;} 
    } 
} 

我在做什麼錯?

+1

找到解決方案。退出該項目,以便我可以選擇適用的工具。例如Moq – Daniel 2012-08-07 15:19:09

回答

7

我認爲使用模擬而不是存根可以解決這個問題,但可能有更好的方法,我錯過了。

 var x = MockRepository.GenerateMock<MyClass>(); 
     x.BackToRecord(BackToRecordOptions.PropertyBehavior); 
     SetupResult.For(x.Items).Return(new List<Item>()); 
     x.Replay(); 
+0

我有同樣的問題,使用Mock也適用於我。不瞭解存根的這個限制。 – 2011-10-03 10:53:14

2

一個更清潔的方式比應該是:

var x = MockRepository.GenerateMock<MyClass>(); 
x.Stub(s => s.Items).Return(new List<Item>()); 

我只是不明白爲什麼GenerateStub不起作用。

+0

可以證實這一點,我相信這是一個錯誤,它不適用於GenerateStub – xhafan 2012-08-07 14:09:15

0

我收到了同樣的信息。我的問題是我試圖在一個具體的類上存儲一個非虛擬屬性。