Mock Registry.GetValue

2 posts, 0 answers
  1. Keith Knight
    Keith Knight avatar
    7 posts
    Member since:
    Feb 2010

    Posted 14 Sep 2010 Link to this post

    I am attempting to mock Registry.GetValue with specific parameters using the following code:

    Mock.Arrange(Function() Registry.GetValue("HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Legacy App\Options""SettingValue"String.Empty)).Returns("Test Result")

    I get the following exception:

    {"Object reference not set to an instance of an object."}
       at ..( parsed, Type argType, Int32 argIndex, Expression argExpression) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\DynamicParser.cs:line 114
       at ..Œ(MethodCallExpression expression) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\DynamicParser.cs:line 94
       at ..ˆ(Expression expression) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\ExpressionVisitor.cs:line 61
       at ..() in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\DynamicParser.cs:line 34
       at ..(Object obj, Type mockTarget, MethodInfo method, Expression expression) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\MockContext.cs:line 31
       at ..(Expression`1 expression) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\MockContext.cs:line 23
       at Telerik.JustMock.Mock...€( x) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\Mock.cs:line 75
       at ..[TDelgate,TReturn](† instruction, Func2`2 function) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\MockContext.cs:line 226
       at Telerik.JustMock.Mock.Arrange[TResult](Expression`1 expression) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\Mock.cs:line 73
       at xxxx.vb

    Is there something that I can do differently?

  2. Ricky
    Ricky avatar
    467 posts

    Posted 16 Sep 2010 Link to this post

    Hi Keith,

    Thank you for submitting the issue. However, I found that there is an issue with expression parsing for VB when you pass string for object type argument. I have added this to the backlog and hopefully it will be fixed in the next release.

    Currently, the workaround for this is to rewrite Mock.Arrange in the following way:

    Mock.Arrange(Function() Registry.GetValue(Arg.AnyString, "SettingValue", Arg.AnyObject)).Returns("Test Result")

    Here, I have used Arg.AnyObject instead of the original string value. Since, this is an MsCorlib member you also need to have a MockClassAttribute and Initialize it during test setup. I have attached a test project for Registry.GetValue which shows it further.

    Additionally, mocking Registry.GetValue works well for test written with NUNit + TestDriven.Net but fails with MsTest where it calls the original method. It seems that MSTest is making call to Registry before running the test and although everthing is setup and injected properly it is calling the original method. Therefore, as of now Registry.GetValue won’t work under MSTest.

    With Regards,

    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 Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft R3 2016 release webinar banner
Back to Top