Unit testing 自动定量记录

Unit testing 自动定量记录,unit-testing,automocking,automoq,Unit Testing,Automocking,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); moc

我开始使用自动吸烟。我试着这样做:

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);
mocker.GetMock();
var line=mocker.Resolve();
line.PropertyOne=.75;
line.PropertyTwo=100;
MyCalc calc=新的MyCalc();
计算乘法(行);
断言.AreEqual(75,行.result);
这运行失败。我的属性没有设置。我错过了自动吸烟的概念吗?什么是好的资源/教程?

要使用Moq设置属性(这是Automoq用于创建模拟对象的功能),必须使用不同的调用,-
Setup
SetupGet
SetupProperty

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);
var line=mocker.Resolve();
//每一个都做同样的事情——“告诉”财产人在得到时返回.75
line.Setup(l=>l.PropertyOne).Returns(.75);
SetupGet(l=>l.PropertyOne).Returns(.75);
line.SetupProperty(l=>l.PropertyOne.75);

我建议在Sut(测试中的系统)中公开结果属性

[TestClass]
公共类SomeTest:ControllerTestBase
{
[测试方法]
public void methodname或subject\u场景或条件\u预期行为或returnvalue()
{
var mock=_autoMoqContainer.GetMock();
变量行=_autoMoqContainer.Resolve();
Setup(x=>x.PropertyOne).Returns(.75);
mock.Setup(x=>x.PropertyTwo).Returns(100);
MyCalc calc=新的MyCalc();
计算乘法(行);
断言等于(75,计算结果);
}
}
公共接口IMYOBJECTTOTLEVEK
{
双属性one{get;set;}
int PropertyTwo{get;set;}
}
公共类MyCalc
{
公共双结果{get;set;}
公共空乘(IMYOBJECTTOT弱线)
{
结果=line.PropertyOne*line.PropertyTwo;
}
}
不相关-但请阅读我关于自动模拟的更多帖子

[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;
    }
}