具有内部构造的Moq混凝土类

时间:2017-08-18 14:43:14

标签: c# moq .net-assembly

我试图让Moq成为一个具有内部构造函数的具体类,即MyAssembly我有

public class MyClass {

    internal MyClass(){}

    // other methods including factory instance method
}

然后在我的TestAssembly'我有

var mock = new Mock<MyClass>();

MyAssembly我已将以下内容添加到AssemblyInfo.cs

[assembly: InternalsVisibleTo("TestAssembly")]

但即使将TestAssembly设为MyAssembly的朋友,Moq仍会抛出错误

Castle.DynamicProxy.InvalidProxyConstructorArgumentsException
Can not instantiate proxy of class: Civica.Metadata.Models.Entities.Stage.
Could not find a parameterless constructor.

Moq能否以这种方式使用内部构造函数创建模拟?

1 个答案:

答案 0 :(得分:4)

请参阅Advanced Features section of the Moq Quickstart

  

模拟另一个项目的内部类型:添加以下程序集   属性(通常是AssemblyInfo.cs)包含的项目   内部类型:

// This assembly is the default dynamic assembly generated Castle DynamicProxy, 
// used by Moq. Paste in a single line. 
[assembly:InternalsVisibleTo("DynamicProxyGenAssembly2,PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]