Moq的具体类带内部构造(Moq Concrete Class with Internal Cons

2019-09-29 08:04发布

我试图最小起订量具有内部构造一个具体的类,即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 ,起订量仍引发错误

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

如果起订量能够创建使用内部构造以这种方式嘲弄?

Answer 1:

见高级功能的起订量快速入门的部分 。

惩戒另一个项目的内部类型:添加以下组件属性(通常到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")]


文章来源: Moq Concrete Class with Internal Constructor