This is a migrated thread and some comments may be shown as answers.

Using ternary operator in definitions of args

1 Answer 60 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Nick
Top achievements
Rank 1
Nick asked on 26 Aug 2014, 12:54 PM
Hi,
The following code does not work...
public interface IService2
{
    string MethodWithArgs(string one, string two);
}
 
[TestMethod]
public void SubbingMethodwithArgs_DontCareAboutValues_StubbedMethodCalled()
{
    var service = StubMethodWithArgs("rv");
    var actual = service.MethodWithArgs("a", "b");
    Assert.AreEqual("rv", actual);
}
 
private static IService2 StubMethodWithArgs(string returnValue, string expectedOne = null, string expectedTwo = null)
{
    var service = Mock.Create<IService2>();
    service
       .Arrange(s => s.MethodWithArgs(
            null != expectedOne ? Arg.Is(expectedOne) : Arg.AnyString,  // null coalesance (??) doesn't work either
            null != expectedTwo ? Arg.Is(expectedTwo) : Arg.AnyString))
       .Returns(returnValue);
    return service;
}


Is this a limitation of JustMock?

Regards,

Nick

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 26 Aug 2014, 01:18 PM
Hi Nick,

JustMock does not understand the use of expressions inside the arrangement expression. For this purpose there's the generalized Arg.Matches() matcher.

Your code can be rewritten like so:
private static IService2 StubMethodWithArgs(string returnValue, string expectedOne = null, string expectedTwo = null)
{
    var service = Mock.Create<IService2>();
    service
       .Arrange(s => s.MethodWithArgs(
            Arg.Matches<string>(v => null != expectedOne ? v == expectedOne : true),
            Arg.Matches<string>(v => null != expectedTwo ? v == expectedTwo : true))
       .Returns(returnValue);
    return service;
}

I hope this helps.

Regards,
Stefan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
General Discussions
Asked by
Nick
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or