Livet er vidunderligt RSS 2.0
# Monday, 26 September 2011

public interface IService
{
    void DoIt(out string a);
}

[TestMethod]
public void Test()
{
    var service = new Mock<IService>();
    var a = "output value";
    service.Setup(s => s.DoIt(out a));

    string b;
    service.Object.DoIt(out b);
    Assert.AreEqual("output value", b);
}


[TestMethod]
public void TestForOutParameterInMoq()
{
    //Arrange
    var _mockParameterManager = new Mock<IParameterManager>();

    Mock<IParameter> mockParameter = new Mock<IParameter>();
    //Parameter affectation should be useless but is not. It's really used by Moq 
    IParameter parameter = mockParameter.Object;

    //Mock method used in UpperParameterManager
    _mockParameterManager.Setup(x => x.OutMethod(out parameter));

    //Act with the real instance
    UpperParameterManager.UpperOutMethod(out parameter);

    //Assert that method used on the out parameter of inner out method are really called
    mockParameter.Verify(x => x.FunctionCalledInOutMethodAfterInnerOutMethod(), Times.Once());

}

[TestMethod]
public void TestForOutParameterInMoq1()
{
    var mock = new Mock<IFoo>();
    // out arguments
    var outString = "ack"; // TryParse will return true, and the out argument will return "ack", lazy evaluated
    mock.Setup(foo => foo.TryParse("ping", out outString)).Returns(true);
    // ref arguments
    var instance = new Bar1(); // Only matches if the ref argument to the invocation is the same instance
    mock.Setup(foo => foo.Submit(ref instance)).Returns(true);
}

[TestMethod]
public void TestForOutParameterInMoq3()
{
    var mock = new Mock<IFoo>();
    mock.Setup(foo => foo.DoSomething("ping")).Returns(true);  
    // out arguments
    var outString = "ack";
    // TryParse will return true, and the out argument will return "ack", lazy evaluated
    mock.Setup(foo => foo.TryParse("ping", out outString)).Returns(true);
  
    // ref arguments
    var instance = new Bar1();
    // Only matches if the ref argument to the invocation is the same instance
    mock.Setup(foo => foo.Submit(ref instance)).Returns(true);  
   
    // access invocation arguments when returning a value
    mock.Setup(x => x.DoSomething(It.IsAny<string>())).Returns((string s) => s.ToLower()); // Multiple parameters overloads available  
    // throwing when invoked
    mock.Setup(foo => foo.DoSomething("reset")).Throws<InvalidOperationException>();
    mock.Setup(foo => foo.DoSomething("")).Throws(new ArgumentException("command");  
    // lazy evaluating return value
    mock.Setup(foo => foo.GetCount()).Returns(() => count);  
    // returning different values on each invocation var mock = new Mock<IFoo>();
    var calls = 0; mock.Setup(foo => foo.GetCountThing()).Returns(() => calls).Callback(() => calls++);
    // returns 0 on first invocation, 1 on the next, and so on
    Console.WriteLine(mock.Object.GetCountThing());

}

Monday, 26 September 2011 22:45:57 (Romance Daylight Time, UTC+02:00)  #    Comments [0] - Trackback
Software Development
Comments are closed.
Archive
<2017 October>
SunMonTueWedThuFriSat
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2017
Christian R. Glinnum
Sign In
Statistics
Total Posts: 128
This Year: 0
This Month: 0
This Week: 0
Comments: 16
Themes
All Content © 2017, Christian R. Glinnum
DasBlog theme 'Business' created by Christoph De Baene (delarou)