Private Variable Mock creates System.MissingMemberException in just mock

1 Answer 200 Views
Non-public Mocking
Chandra
Top achievements
Rank 1
Chandra asked on 16 Sep 2021, 03:08 PM

Hi,

we have a class like this.

 

public class MssqlDbConnection : ISecuredDatabaseConnection, IDisposable
    {
        #region Fields
        private SqlConnection _connection = null;

     }

 

And I am creating Private Mock like this

             SqlConnection Sql = new SqlConnection();
            // Connected Mock
            Mock.NonPublic.Arrange<SqlConnection>(typeof(MssqlDbConnection), "_connection").Returns(Sql);

This Statement causes crash with System.MissingMemberException in just mock

 

Please let me know on this.

 

Regards,

Chandra.

1 Answer, 1 is accepted

Sort by
0
Ivo
Telerik team
answered on 17 Sep 2021, 11:48 AM

Hello Chandra,

JustMock is incapable to mock fields and that is the root cause of the issue. However, there is a relatively easy workaround: use a property instead of the field, which could be successfully mocked.

Regards,
Ivo
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Chandra
Top achievements
Rank 1
commented on 04 Oct 2021, 09:57 AM

Thanks Ivo.

 

Ivo
Telerik team
commented on 04 Oct 2021, 10:47 AM

You are welcome, Chandra! If you are accepting the answer please mark it, so the others having the same issue could benefit from this solution. Wish you a great day!
Tags
Non-public Mocking
Asked by
Chandra
Top achievements
Rank 1
Answers by
Ivo
Telerik team
Share this question
or