Using ternary operator in definitions of args

2 posts, 0 answers
  1. Nick
    Nick avatar
    3 posts
    Member since:
    Jan 2012

    Posted 26 Aug 2014 Link to this post

    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
  2. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 26 Aug 2014 Link to this post

    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.

     
  3. DevCraft R3 2016 release webinar banner
Back to Top