带内部构造函数的 Moq 混凝土类 [英] Moq Concrete Class with Internal Constructor

查看:24
本文介绍了带内部构造函数的 Moq 混凝土类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 Moq 一个具有内部构造函数的具体类,即在 MyAssembly 我有

I'm attempting to Moq a concrete class that has an internal constructor, i.e., in MyAssembly I have

public class MyClass {

    internal MyClass(){}

    // other methods including factory instance method
}

然后在我的TestAssembly"测试中

then in my test in `TestAssembly' I have

var mock = new Mock<MyClass>();

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

in MyAssembly I have added the following to AssemblyInfo.cs

[assembly: InternalsVisibleTo("TestAssembly")]

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

but even setting TestAssembly to be a friend of MyAssembly, Moq still throws the error

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

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

Should Moq be able to create mocks using internal constructors in this manner?

推荐答案

参见 Moq 快速入门的高级功能部分.

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

Mocking internal types of another project: add the following assembly attribute (typically to the AssemblyInfo.cs) to the project containing the internal types:

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

这篇关于带内部构造函数的 Moq 混凝土类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆