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

Mock.NonPublic.Arrange with inherited non-overridden members doesn't seem to work.

1 Answer 46 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Charles
Top achievements
Rank 1
Charles asked on 08 Mar 2011, 05:03 AM
I just started using JustMock but I've searched for a solution to this problem and haven't found it yet so I'll pose the problem here.
As in the distilled example below, I have a class that inherits from another class.  The parent class defines multiple protected virtual methods that the child class does not override.  I'm interested in testing a specific protected virtual method the child does implement but my attempts to mock the non-overridden parent methods fail with the following exception:

[System.ArgumentException] = {"Could not resolve the target method; make sure that you have provided arguments correctly."}

(In the example, it is thrown on the call to Mock.NonPublic.Arrange(..) )

Am I doing something wrong? Is there a way within JustMock to get around this error? Or must I subclass my child class further and override all the methods I'm interested in mocking in order to get it to work?  That last option does work, but it's not exactly ideal.
Any help is greatly appreciated!

Charles

public class Foo
{
    public Foo()
    {
    }
 
    protected virtual void MethodToMock()
    {
    }
 
    protected virtual void MethodToTest()
    {
    }
}
 
 
public class Bar : Foo
{
    public Bar()
    {
    }
 
    protected override void MethodToTest()
    {
        MethodToMock();
    }
}
 
 
[TestMethod]
public void BarTest()
{
    var mock = Mock.Create<Bar>(Behavior.CallOriginal);
    Mock.NonPublic.Arrange(mock, "MethodToMock").DoNothing();
 
    // Test MethodToTest here
}

1 Answer, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 09 Mar 2011, 09:12 AM
Hi Charles,

Thanks again for sending the issue. You are right about the problem. It should not throw any exception when mocking non virtual protected member from base class. However, this is issue is fixed and will go out with the Q1 build in few days.


Sorry for the inconvenience and please do feel free to report any further exceptions that you might encounter or might seem inappropriate to you. We would love to assists you.


Kind Regards,
Ricky
the Telerik team

Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
General Discussions
Asked by
Charles
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Share this question
or