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

Failure of recursive mocking of internal type.

9 Answers 81 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Bob
Top achievements
Rank 1
Bob asked on 28 Mar 2012, 01:59 PM
I think I have discovered a bug.
    internal class InternalClass
    {
        public int GetInt() { return 5; }
    }
 
    internal class InternalOwner
    {
        public int GetInt() { return 5; }
        public InternalClass InternalClassField = new InternalClass();
 
        public InternalClass InternalClassProp
        {
            get { return InternalClassField; }
        }
    }
 
...
 
    [Test]
    public void Test5()
    {
        InternalOwner io = Mock.Create<InternalOwner>();
//      Mock.Arrange(() => io.GetInt()).Returns(6);     // Test1, works.
//      Mock.Arrange(() => io.InternalClassField.GetInt()).Returns(6);      // Test2, works.
        Mock.Arrange(() => io.InternalClassProp.GetInt()).Returns(6);       // Test3, FAILS.
    }
If you uncomment each of the lines Test1, Test2 and Test3 in turn you'll see that recursively mocking the GetInt() method via InternalClassProp fails.

Adding the InternalsVisibleto assembly attribute has no effect.

9 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 30 Mar 2012, 04:29 PM
Hi Bob,
Thanks again for reporting the issue. However, in your test I found that you have the following line:
Mock.Arrange(() => io.InternalClassField.GetInt()).Returns(6); .

This is invalid anyway since you can't mock a field as there is no way to intercept it. Next, adding the InternalsVisibleTo attribute from the following post made it work.

http://www.telerik.com/help/justmock/basic-usage-mock-internal-types-via-proxy.html

I have also included the test project to let you have a look. However, I found an exception accessing the InternalClass from this line:
Assert.AreEqual(6, io.InternalClassProp.GetInt());

For the above case, i am creating a task at PITS which you can follow here:
http://www.telerik.com/support/pits.aspx#/public/justmock/10540

I am also adding some telerik points into your account for your awesome support.

Kind Regards,
Mehfuz
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Bob
Top achievements
Rank 1
answered on 30 Mar 2012, 05:40 PM
I've had a look at your sample and compared that solution to my project. I noticed that you were using MsTest as the unit test framework and I was using Nunit, after changing the unit test framework to MsTest I confirm it works exactly as you have stated. Adding the InternalsVisibleAttribute does indeed enable the Arrange call to succeed but accessing the internal class throws an exception.

Test 'MsTestApp1.TestSuite1.Test6' failed: Test method MsTestApp1.TestSuite1.Test6 threw exception:
System.TypeAccessException: Attempt by method 'InternalOwner_Interceptor_fdecbacbc82243e184fd89253931c3c2.Intercept(InternalOwner, Boolean ByRef)' to access type 'MsTestApp1.TestSuite1+InternalClass' failed.
    at InternalOwner_Interceptor_fdecbacbc82243e184fd89253931c3c2.Intercept(InternalOwner , Boolean& )
    D:\_UserData\Development\Workspaces\CSharpTesting\JustMockTesting\MsTestApp1\TestSuite1.cs(25,0): at MsTestApp1.TestSuite1.InternalOwner.get_InternalClassProp()
    D:\_UserData\Development\Workspaces\CSharpTesting\JustMockTesting\MsTestApp1\TestSuite1.cs(60,0): at MsTestApp1.TestSuite1.Test6()

I hope you can get that fixed quickly as it's a bit of a showstopper.

Don't you support mocking via Nunit? That's probably not a showstopper but Nunit has a good reputation (and I've never had any problems with it before) whereas most TDD guys suggest MsTest is pretty vile.
0
Ricky
Telerik team
answered on 03 Apr 2012, 07:42 PM
Hi Bob,
Thanks again for the reply. I am attaching the updated project with NUnit for your convenience and yes JustMock officially supports NUnit.
 
In addition, we are also looking into the issue mentioned in the following PITS entry and hopefully it will be fixed in coming release.
 
http://www.telerik.com/support/pits.aspx#/public/justmock/10540

Kind regards,
Mehfuz
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Bob
Top achievements
Rank 1
answered on 04 Apr 2012, 10:12 AM
Thanks. It's such a critical feature for us though that we are holding off on further testing of JustMock until the fix for recursive mocking internal types is deployed. If you get a fix and want to push an interim build my way for testing that would be great, I'll be monitoring this thread and the PITs ticket for updates.
0
Ricky
Telerik team
answered on 06 Apr 2012, 04:50 PM
Hi Bob,
Thanks again for your reply. I will try to prepare an internal build next week once the issue is fixed. In the meantime, should you face any other problem please don't hesitate to contact me.

Kind Regards
Mehfuz
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Bob
Top achievements
Rank 1
answered on 17 Apr 2012, 09:47 AM
Can you give me a quick update on the status of this issue please?
0
Ricky
Telerik team
answered on 19 Apr 2012, 09:27 PM
Hi Bob,

Thanks again for contacting us. I am still working on the issue. Hopefully I will send you a build early next week.

Thank you for your patience.

Kind Regards
Mehfuz 
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Bob
Top achievements
Rank 1
answered on 11 May 2012, 01:37 PM
Any progress on this yet please?
0
Ricky
Telerik team
answered on 16 May 2012, 05:03 PM
Hi Bob,

Thanks again for contacting us. 

I am having some issues in fixing the bug. However, we have fixed a similar issue in SP1 that I would highly recommend to check out. In addition, I will hopefully get around the problem and send you a build next week, also let you know for any exceptions.  

In addition, you can always reach me at mehfuz.hossain@telerik.com


Kind Regards
Mehfuz
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
General Discussions
Asked by
Bob
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Bob
Top achievements
Rank 1
Share this question
or