2

I am getting started with Automoq. I was trying to do something like this:

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);

This runs bu fails. My properties do not get set. Am I missing the idea of Automoq? What is a good resource/tutorial?

JChris
  • 41
  • 3

2 Answers2

0

To set property with Moq (this is what Automoq uses to create mock objects) you have to use different calls, - Setup, SetupGet or 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);
k.m
  • 30,794
  • 10
  • 62
  • 86
0

I suggest expose a Result property in your Sut (System Under test)

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

Not related - But read my post more on AutoMocking http://www.dotnetcurry.com/ShowArticle.aspx?ID=767

Spock
  • 7,009
  • 1
  • 41
  • 60