2013-06-04 8 views
1

我開始使用Automoq。我試圖做這樣的事情:Automoq文檔

mocker.GetMock<IMyObjectToTweak>(); 
var line = mocker.Resolve<IMyObjectToTweak>(); 

line.PropertyOne = .75; 
line.PropertyTwo = 100; 

MyCalc calc = new MyCalc(); 
calc.Multiply(line); 
Assert.AreEqual(75, line.result); 

這運行BU失敗。我的屬性沒有設置。我錯過了Automoq的想法嗎?什麼是一個很好的資源/教程?

回答

0

設置屬性與MOQ(這是Automoq用於創建mock對象),你必須使用不同的電話, - SetupSetupGetSetupProperty

var line = mocker.Resolve<IMyObjectToTweak>(); 
// each does the same thing - "tells" PropertyOne to return .75 upon get 
line.Setup(l => l.PropertyOne).Returns(.75); 
line.SetupGet(l => l.PropertyOne).Returns(.75); 
line.SetupProperty(l => l.PropertyOne, .75); 
0

我建議你蘇特暴露出Result屬性(待測系統)

[TestClass] 
public class SomeTest : ControllerTestBase 
{ 
    [TestMethod] 
    public void MethodNameOrSubject_ScenarioOrCondition_ExpectedBehaviourOrReturnValue() 
    { 
     var mock = _autoMoqContainer.GetMock<IMyObjectToTweak>(); 
     var line = _autoMoqContainer.Resolve<IMyObjectToTweak>(); 

     mock.Setup(x => x.PropertyOne).Returns(.75); 
     mock.Setup(x => x.PropertyTwo).Returns(100); 

     MyCalc calc = new MyCalc(); 
     calc.Multiply(line); 
     Assert.AreEqual(75, calc.Result); 
    } 
} 

public interface IMyObjectToTweak 
{ 
    double PropertyOne { get; set; } 
    int PropertyTwo { get; set; } 

} 

public class MyCalc 
{ 
    public double Result { get; set; } 

    public void Multiply(IMyObjectToTweak line) 
    { 
     Result = line.PropertyOne*line.PropertyTwo; 
    } 
} 

沒有關係 - 但閱讀我的文章更上AutoMocking http://www.dotnetcurry.com/ShowArticle.aspx?ID=767