Can I handle parameter like the case insensitive string?

3 posts, 0 answers
  1. Andrey
    Andrey avatar
    4 posts
    Member since:
    Dec 2010

    Posted 09 Aug Link to this post

     

    I have the class under test:

    1.public class AppManager {
    2.    public string[] GetAppSets() => Registry.LocalMachine
    3.        .OpenSubKey(@"SOFTWARE\Autodesk\AutoCAD", false)
    4.        ?.GetSubKeyNames();
    5.}

    Also, I have the test for `GetAppSets` method:

    01.[Test]
    02.public void Foo_Returns_ValidValue() {
    03. 
    04.    const string subkey = @"SOFTWARE\Autodesk\AutoCAD";
    05.    /* The sets of applications which are based on
    06.     * AutoCAD 2009-2017. */
    07.    string[] fakeSets = new[] { "R17.2", "R18.0",
    08.        "R18.1", "R18.2", "R19.0", "R19.1", "R20.0",
    09.        "R20.1","R21.0" };
    10. 
    11.    RegistryKey rk = Mock.Create<RegistryKey>();
    12. 
    13.    Mock.Arrange(() => rk.GetSubKeyNames()).Returns(
    14.        fakeSets);
    15. 
    16.    Mock.Arrange(() => Registry.LocalMachine.OpenSubKey
    17.    (subkey, false)).Returns(rk);
    18. 
    19.    AppManager appMng = new AppManager();
    20.    string[] appSets = appMng.GetAppSets();
    21. 
    22.    Assert.AreEqual(fakeSets, appSets);
    23.}

    It works. But my test will be failure if `GetAppSets` method uses "Software\Autodesk\AutoCAD" or "software\autodesk\autocad" string instead of "SOFTWARE\Autodesk\AutoCAD".

    So, at this case I need to handle parameter like the case insensitive string. Is it possible?

     

     

  2. Andrey
    Andrey avatar
    4 posts
    Member since:
    Dec 2010

    Posted 09 Aug in reply to Andrey Link to this post

    The addition info: the `appSets` variable will be `null` if string case will be changed (because that registry key doesn't exist on my computer).
  3. DevCraft R3 2016 release webinar banner
  4. Svetlozar
    Admin
    Svetlozar avatar
    269 posts

    Posted 12 Aug Link to this post

    Hi,

    Thank you for the great question! 

    So, at this case I need to handle parameter like the case insensitive string. Is it possible?
    Yes, you can get flexible with our matcher using the Arg.Matches mechanism. For more information you can have a look at this blog post - Handling Arguments in JustMock Arrangements, more specifically - the last paragraph - Using Lambdas for Argument Matching.

    In your case I guess that would do the job 
    Mock.Arrange(() => Registry.LocalMachine.OpenSubKey(Arg.Matches<string>(s => s.ToLower() == subkey.ToLower()), false)).Returns(rk);

    Let us know if you have more questions.

    Regards,
    Svetlozar
    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top