单元/集成测试中的反射问题

时间:2021-04-30 15:39:51

I´m dynamically creating an instance of a class with reflection and this works fine, except when trying to do this through unit testing - I´m using the MS testing framework.
I get the familiar error of: "Could not load file or assembly 'Assy' or one of its dependencies. The system cannot find the file specified"
I have copied the dll into the bin\debug bin of the Unit test project - is this not the correct place to put it?

我正在动态创建一个带反射的类的实例,这种方法很好,除非通过单元测试尝试这样做 - 我使用的是MS测试框架。我得到了一个熟悉的错误:“无法加载文件或程序集'Assy'或其中一个依赖项。系统找不到指定的文件”我已将dll复制到Unit测试项目的bin \ debug bin中 - 这是不是正确的地方吗?

string assyName = "Go.Data.SqlServer";
string typeName = "GoMolaMola.Data.SqlServer.DataProviderFactory";

Assembly assy = Assembly.Load( assyName );
object o = assy.CreateInstance( typeName );

Any ideas? I'm new to unit testing and any help would be appreciated.

有任何想法吗?我是单位测试的新手,任何帮助将不胜感激。

Thanks

3 个解决方案

#1


The bin/Debug folder is not where the unit tests run. Visual Studio will copy the output of your unit test compilation to a TestResults folder (typically keeping the last five test runs, each with a timestamp embedded in the folder name) and run the unit tests there.

bin / Debug文件夹不是运行单元测试的位置。 Visual Studio会将单元测试编译的输出复制到TestResults文件夹(通常保留最后五次测试运行,每个测试运行时都会在文件夹名称中嵌入时间戳)并在那里运行单元测试。

If you want the .DLL in that folder, either create a reference to the .DLL from your test project, or use the DeploymentItem attribute to make sure the item is copied to the test directory.

如果要在该文件夹中使用.DLL,请从测试项目创建对.DLL的引用,或使用DeploymentItem属性确保将项目复制到测试目录。

#2


I faced this problem too and none of the above answer worked for me :( 1. Adding reference to project doesn't work for me 2. Adding the DeploymentItem attribute also doesn't work 3. Adding the Post-Build command is also not possible in this case as Unit test engine is creating a new out directory each time with time stamp....and its searching that assembly in this new directory.

我也遇到了这个问题,上面的答案都没有对我有用:( 1.添加对项目的引用对我不起作用2.添加DeploymentItem属性也不起作用3.添加Post-Build命令也不行可能在这种情况下,单元测试引擎每次都使用时间戳创建一个新的输出目录....并在此新目录中搜索该程序集。

but I managed to solve this by enabling deployment and adding the specified file in Local Test Setting -->Deployment

但我设法通过启用部署并在本地测试设置 - >部署中添加指定的文件来解决这个问题

#3


For cases like this, when loading the DLL dynamically is needed from the Unit Testing, I have a post-build event that copies the DLL to that directory. I'd love to know if there's another way to do it. That was the only way that worked for me :(

对于这样的情况,当单元测试需要动态加载DLL时,我有一个将DLL复制到该目录的构建后事件。我很想知道是否有另一种方法可以做到这一点。这是对我有用的唯一方式:(

To edit a Post-Build, right-click over the project, go to Build events, and place the copy, like this, in the Post-Build event command line:

要编辑Post-Build,右键单击项目,转到Build events,然后将副本放在Post-Build事件命令行中:

copy $(TargetPath) "$(SolutionDir)yourDir\$(TargetFileName)"

#1


The bin/Debug folder is not where the unit tests run. Visual Studio will copy the output of your unit test compilation to a TestResults folder (typically keeping the last five test runs, each with a timestamp embedded in the folder name) and run the unit tests there.

bin / Debug文件夹不是运行单元测试的位置。 Visual Studio会将单元测试编译的输出复制到TestResults文件夹(通常保留最后五次测试运行,每个测试运行时都会在文件夹名称中嵌入时间戳)并在那里运行单元测试。

If you want the .DLL in that folder, either create a reference to the .DLL from your test project, or use the DeploymentItem attribute to make sure the item is copied to the test directory.

如果要在该文件夹中使用.DLL,请从测试项目创建对.DLL的引用,或使用DeploymentItem属性确保将项目复制到测试目录。

#2


I faced this problem too and none of the above answer worked for me :( 1. Adding reference to project doesn't work for me 2. Adding the DeploymentItem attribute also doesn't work 3. Adding the Post-Build command is also not possible in this case as Unit test engine is creating a new out directory each time with time stamp....and its searching that assembly in this new directory.

我也遇到了这个问题,上面的答案都没有对我有用:( 1.添加对项目的引用对我不起作用2.添加DeploymentItem属性也不起作用3.添加Post-Build命令也不行可能在这种情况下,单元测试引擎每次都使用时间戳创建一个新的输出目录....并在此新目录中搜索该程序集。

but I managed to solve this by enabling deployment and adding the specified file in Local Test Setting -->Deployment

但我设法通过启用部署并在本地测试设置 - >部署中添加指定的文件来解决这个问题

#3


For cases like this, when loading the DLL dynamically is needed from the Unit Testing, I have a post-build event that copies the DLL to that directory. I'd love to know if there's another way to do it. That was the only way that worked for me :(

对于这样的情况,当单元测试需要动态加载DLL时,我有一个将DLL复制到该目录的构建后事件。我很想知道是否有另一种方法可以做到这一点。这是对我有用的唯一方式:(

To edit a Post-Build, right-click over the project, go to Build events, and place the copy, like this, in the Post-Build event command line:

要编辑Post-Build,右键单击项目,转到Build events,然后将副本放在Post-Build事件命令行中:

copy $(TargetPath) "$(SolutionDir)yourDir\$(TargetFileName)"