Mocking issue: Can not instantiate proxy of class. Could not find a parameterless constructor
up vote
0
down vote
favorite
I am trying to write unit test for a class which I can not change implementation.
Below is the error which I am seeing while writing tests.
Unit to Mock:
public class ClassA : IInterface
{
public readonly ClassB _classB = null;
public ClassA(ClassB classB)
{
_classB = classB;
}
private ClassP<ClassB, ClassC> _classP;
public virtual ClassP<ClassB, ClassC> TargetToMock => _classP ?? (_classP = new ClassP<ClassB, ClassC>(_classB));
}
Mocking:
MockClassB = new Mock<ClassB>();
MockClassA = new Mock<ClassA>(MockClassB.Object);
Setup:
MockClassA
.Setup(s => s.TargetToMock.SomeMethod(argument1, argument2))
.Returns(SomeResponseObject);
Exception:
Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : Can
not instantiate proxy of class: ClassP`2[[ClassB, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null],[ClassC, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null]].
Could not find a parameterless constructor.
c# .net unit-testing mocking moq
New contributor
sillydeveloper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
0
down vote
favorite
I am trying to write unit test for a class which I can not change implementation.
Below is the error which I am seeing while writing tests.
Unit to Mock:
public class ClassA : IInterface
{
public readonly ClassB _classB = null;
public ClassA(ClassB classB)
{
_classB = classB;
}
private ClassP<ClassB, ClassC> _classP;
public virtual ClassP<ClassB, ClassC> TargetToMock => _classP ?? (_classP = new ClassP<ClassB, ClassC>(_classB));
}
Mocking:
MockClassB = new Mock<ClassB>();
MockClassA = new Mock<ClassA>(MockClassB.Object);
Setup:
MockClassA
.Setup(s => s.TargetToMock.SomeMethod(argument1, argument2))
.Returns(SomeResponseObject);
Exception:
Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : Can
not instantiate proxy of class: ClassP`2[[ClassB, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null],[ClassC, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null]].
Could not find a parameterless constructor.
c# .net unit-testing mocking moq
New contributor
sillydeveloper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I think the correct way isMockClassA = new ClassA(MockClassB.Object)
– Kien Chu
Nov 5 at 2:00
you are right, actually I have a typo in the question
– sillydeveloper
Nov 5 at 2:18
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to write unit test for a class which I can not change implementation.
Below is the error which I am seeing while writing tests.
Unit to Mock:
public class ClassA : IInterface
{
public readonly ClassB _classB = null;
public ClassA(ClassB classB)
{
_classB = classB;
}
private ClassP<ClassB, ClassC> _classP;
public virtual ClassP<ClassB, ClassC> TargetToMock => _classP ?? (_classP = new ClassP<ClassB, ClassC>(_classB));
}
Mocking:
MockClassB = new Mock<ClassB>();
MockClassA = new Mock<ClassA>(MockClassB.Object);
Setup:
MockClassA
.Setup(s => s.TargetToMock.SomeMethod(argument1, argument2))
.Returns(SomeResponseObject);
Exception:
Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : Can
not instantiate proxy of class: ClassP`2[[ClassB, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null],[ClassC, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null]].
Could not find a parameterless constructor.
c# .net unit-testing mocking moq
New contributor
sillydeveloper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I am trying to write unit test for a class which I can not change implementation.
Below is the error which I am seeing while writing tests.
Unit to Mock:
public class ClassA : IInterface
{
public readonly ClassB _classB = null;
public ClassA(ClassB classB)
{
_classB = classB;
}
private ClassP<ClassB, ClassC> _classP;
public virtual ClassP<ClassB, ClassC> TargetToMock => _classP ?? (_classP = new ClassP<ClassB, ClassC>(_classB));
}
Mocking:
MockClassB = new Mock<ClassB>();
MockClassA = new Mock<ClassA>(MockClassB.Object);
Setup:
MockClassA
.Setup(s => s.TargetToMock.SomeMethod(argument1, argument2))
.Returns(SomeResponseObject);
Exception:
Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : Can
not instantiate proxy of class: ClassP`2[[ClassB, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null],[ClassC, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null]].
Could not find a parameterless constructor.
c# .net unit-testing mocking moq
c# .net unit-testing mocking moq
New contributor
sillydeveloper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
sillydeveloper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Nov 5 at 2:20
New contributor
sillydeveloper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Nov 5 at 1:57
sillydeveloper
11
11
New contributor
sillydeveloper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
sillydeveloper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
sillydeveloper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I think the correct way isMockClassA = new ClassA(MockClassB.Object)
– Kien Chu
Nov 5 at 2:00
you are right, actually I have a typo in the question
– sillydeveloper
Nov 5 at 2:18
add a comment |
I think the correct way isMockClassA = new ClassA(MockClassB.Object)
– Kien Chu
Nov 5 at 2:00
you are right, actually I have a typo in the question
– sillydeveloper
Nov 5 at 2:18
I think the correct way is
MockClassA = new ClassA(MockClassB.Object)– Kien Chu
Nov 5 at 2:00
I think the correct way is
MockClassA = new ClassA(MockClassB.Object)– Kien Chu
Nov 5 at 2:00
you are right, actually I have a typo in the question
– sillydeveloper
Nov 5 at 2:18
you are right, actually I have a typo in the question
– sillydeveloper
Nov 5 at 2:18
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
sillydeveloper is a new contributor. Be nice, and check out our Code of Conduct.
sillydeveloper is a new contributor. Be nice, and check out our Code of Conduct.
sillydeveloper is a new contributor. Be nice, and check out our Code of Conduct.
sillydeveloper is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147359%2fmocking-issue-can-not-instantiate-proxy-of-class-could-not-find-a-parameterles%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I think the correct way is
MockClassA = new ClassA(MockClassB.Object)– Kien Chu
Nov 5 at 2:00
you are right, actually I have a typo in the question
– sillydeveloper
Nov 5 at 2:18