我正在尝试将 moles 与 NUnit 一起使用,并收到以下错误“Moles 需要测试是一个仪表化的过程”。我还在 Visual Studio 2008 中使用 Visual NUnit 来完成这项工作。欢迎任何帮助。
5 回答
这就是我为了让 Moles 与 NUnit 一起工作所做的:
获取存档
C:\Program Files (x86)\Microsoft Moles\Documentation\moles.samples.zip
并提取Moles
解决方案文件夹。NUnit
在 Visual Studio (2008) 中构建项目以供发布。将输出文件复制
Microsoft.Moles.NUnit.dll
到. 我怀疑重新编译 NUnit 插件而不是使用来自下载和安装的插件的这一步骤是实际的解决点。Microsoft.Moles.NUnit.xml
...\Moles\NUnit\bin\Release\
C:\Program Files (x86)\NUnit 2.5.9\bin\net-2.0\addins\
在您的 VS 测试项目中,确保添加对
C:\Program Files (x86)\NUnit 2.5.9\bin\net-2.0\addins\Microsoft.Moles.NUnit.dll
刚刚复制的引用。在您的 VS 测试类中,使用
[Moled]
属性标记测试方法(这将需要一个using Microsoft.Moles.Framework.NUnit
)。作为替代方案,将其实现包装在一个using (MolesContext.Create()) { ... }
块中(这将需要一个using Microsoft.Moles.Framework
)。在命令行中,通过 moles 运行器调用 NUnit 测试运行器,使用以下命令:
"C:\Program Files (x86)\Microsoft Moles\bin\moles.runner.exe" "path\to\your\test\assembly.dll" /r:"C:\Program Files (x86)\NUnit 2.5.9\bin\net-2.0\nunit-console.exe" /x86 /args:"/domain=None"
我发现 [Moled] 属性不适用于 [TestCase(...)] ,它让您回到未检测的错误场景。相反,该using (MolesContext.Create())
块也适用于这种情况。
一旦你发现一切正常,你可能会考虑在 Visual Studio 中将 moles runner 作为外部工具运行。按照使用 Visual Studio 中的 NUnit 控制台运行 Moles 中的说明,更新参数,如步骤 6 所示。
请注意,这是在 Windows 7 64 位机器上使用 NUnit 2.5.9、Microsoft Pex 和 Moles (x86) 0.94.51006.1。考虑不同路径、版本等的实际文件夹。
我正在使用 Moles 版本 0.94.51023.0。
据我所知,您需要将以下属性添加到您的测试方法中。我正在使用 Visual Studio 测试框架,不确定它是否与 NUnit 等相同。人。
[HostType("Moles")]
我还读到您可以将[Moled]
属性添加到测试方法中,但这对我来说是不可用的,而且我没有说明原因,假设它是旧文档 - Moles 似乎有很多。
更新:根据 Moles 参考手册,pg。26、测试方法上的MoledAttribute是NUnit走的路。您必须通过将Microsoft.Moles.NUnit.dll程序集复制到 NUnit bin/addins文件夹来向 NUnit 注册它。
然后,您将该[Moled]
属性添加到测试方法中。
[Test]
[Moled]
public void MyTest() {
...
}
否则,您将添加一个 using 块来包装测试方法,如下所示:
[Test]
public void MyTest() {
using (MolesContext()) {
...
}
}
除了添加 [HostType("Moles")] 属性外,您还需要用 moles runner 包裹测试 runner。例如:
moles.runner.exe MyAssembly.dll /r:nunit-console.exe
Moles runner 可能位于C:\Program Files\Microsoft Moles\bin中。要使用,请执行:
moles.runner.exe help
superjos 有最正确的答案,使用“持续测试”插件,您可以让 Visual Studio 使用此批处理文件通过 NUnit 控制台运行器运行 Moles 运行器:
@echo off
rem Uses the Microsoft Moles runner and fires off the NUnit console runner so you can use Moles with NUnit.
rem This batch file is intended to be run from the Continuous Testing plugin in Visual Studio.
rem However, it can be used to run nunit tests from anyhere with Moles as long as the first parameter
rem is the assembly to be tested. All other parameters are passed to NUnit.
set ASSEMBLY=%1
set MOLESPATH="c:\Program Files\Microsoft Moles\bin\moles.runner.exe"
set NUNITPATH="C:\Program Files\NUnit 2.5.10\bin\net-2.0\nunit-console.exe"
shift
if [%ASSEMBLY%]==[] goto HelpScreen
if [%1]==[] goto RunAlone
if [%2]==[] goto RunParams1
if [%3]==[] goto RunParams2
if [%4]==[] goto RunParams3
if [%5]==[] goto RunParams4
if [%6]==[] goto RunParams5
if [%7]==[] goto RunParams6
if [%8]==[] goto RunParams7
if [%9]==[] goto RunParams8
goto RunParams9
:HelpScreen
echo "The parameters are the same as NUnit Console runner with the exception that:
echo " 1) Only one assembly is supported and it must come first"
echo " 2) Only 9 extra parameters may be specified"
echo
%NUNITPATH% /?
exit 1
:RunAlone
%MOLESPATH% /r:%NUNITPATH% %ASSEMBLY%
goto ExitRunner
:RunParams1
%MOLESPATH% /r:%NUNITPATH% %ASSEMBLY% /args:%1
goto ExitRunner
:RunParams2
%MOLESPATH% /r:%NUNITPATH% %ASSEMBLY% /args:%1 /args:%2
goto ExitRunner
:RunParams3
%MOLESPATH% /r:%NUNITPATH% %ASSEMBLY% /args:%1 /args:%2 /args:%3
goto ExitRunner
:RunParams4
%MOLESPATH% /r:%NUNITPATH% %ASSEMBLY% /args:%1 /args:%2 /args:%3 /args:%4
goto ExitRunner
:RunParams5
%MOLESPATH% /r:%NUNITPATH% %ASSEMBLY% /args:%1 /args:%2 /args:%3 /args:%4 /args:%5
goto ExitRunner
:RunParams6
%MOLESPATH% /r:%NUNITPATH% %ASSEMBLY% /args:%1 /args:%2 /args:%3 /args:%4 /args:%5 /args:%6
goto ExitRunner
:RunParams7
%MOLESPATH% /r:%NUNITPATH% %ASSEMBLY% /args:%1 /args:%2 /args:%3 /args:%4 /args:%5 /args:%6 /args:%7
goto ExitRunner
:RunParams8
%MOLESPATH% /r:%NUNITPATH% %ASSEMBLY% /args:%1 /args:%2 /args:%3 /args:%4 /args:%5 /args:%6 /args:%7 /args:%8
goto ExitRunner
:RunParams9
%MOLESPATH% /r:%NUNITPATH% %ASSEMBLY% /args:%1 /args:%2 /args:%3 /args:%4 /args:%5 /args:%6 /args:%7 /args:%8 /args:%9
goto ExitRunner
:ExitRunner
只需更新软件包版本的路径即可。如果您有时间更新它,这也可以用于从其他程序运行它。