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

Can I handle parameter like the case insensitive string?

2 Answers 82 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Андрей
Top achievements
Rank 1
Андрей asked on 09 Aug 2016, 09:41 AM

 

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 Answers, 1 is accepted

Sort by
0
Андрей
Top achievements
Rank 1
answered on 09 Aug 2016, 10:38 AM
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).
0
Svetlozar
Telerik team
answered on 12 Aug 2016, 08:39 AM
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
Tags
General Discussions
Asked by
Андрей
Top achievements
Rank 1
Answers by
Андрей
Top achievements
Rank 1
Svetlozar
Telerik team
Share this question
or