简体   繁体   English

使用TestDriven.NET和NUnit时FileNotFound异常

[英]FileNotFound Exception when using TestDriven.NET and NUnit

I'm Writing a simple pong game in C# and XNA 4.0 to learn unit testing. 我正在用C#和XNA 4.0编写一个简单的Pong游戏,以学习单元测试。 The tools used are TestDriven.NET and NUnit, all newest versions. 使用的工具是TestDriven.NET和NUnit,所有最新版本。 The problem is, if I test the code with VS2010 internal debugger, everything runs fine, but when I use "Run Test(s)" from menu, the application chokes with error: 问题是,如果我使用VS2010内部调试器测试代码,一切运行正常,但是当我从菜单中使用“运行测试”时,应用程序会因错误而阻塞:

Test 'WindowsGame1.Game1.TestGameMenu' failed: Microsoft.Xna.Framework.Content.ContentLoadException : Error loading "SpaceBackground". File not found.
----> System.IO.FileNotFoundException : Error loading "Content\SpaceBackground.xnb". File not found.
(some more below...)

So it stops when the first textre is going to be loaded. 因此,它将在第一个textre加载时停止。 What's going on? 这是怎么回事?

If you want to check the code out, download it here http://www.mediafire.com/?qwnkmyqheum 如果您想签出代码,请在此处下载代码http://www.mediafire.com/?qwnkmyqheum

The obvious question here is what the working directory is, when TD.NET runs the test. 显而易见的问题是,当TD.NET运行测试时,工作目录是什么。 If your files are in C:\\game1\\content\\* , and TD.NET runs the tests while operating in C:\\TDNET (or whatever), then obviously trying to load Content\\Whatever.jpg is going to try and open C:\\TDNET\\Content\\Whatever.jpg , which doesn't exist. 如果您的文件位于C:\\game1\\content\\* ,并且TD.NET在C:\\TDNET (或其他任何版本)中运行时运行测试,则显然尝试加载Content\\Whatever.jpg将尝试打开C:\\TDNET\\Content\\Whatever.jpg ,不存在。 The simple workaround would be to set an absolute path for your content files (if only temporarily) to verify that this is the problem. 一种简单的解决方法是为您的内容文件设置一个绝对路径(如果只是临时设置)以验证这是问题所在。

The other question is, what version of TestDriven.net are you using? 另一个问题是,您正在使用哪个版本的TestDriven.net? Their release notes from 2.7 say this: 他们从2.7发布的说明中说:

966: Problems with XNA tests (working directory issues) 966:XNA测试问题(工作目录问题)

See here and here for workaround. 解决方法请参见此处此处

The first site links to the second one, which offers this workaround: 第一个站点链接到第二个站点,第二个站点提供以下解决方法:

Step 1. 第1步。

Download AssemblyUtilities.zip and unpack into your project and add it to your solution. 下载AssemblyUtilities.zip并解压缩到您的项目中,然后将其添加到您的解决方案中。

Step 2. 第2步。

For every ad hoc test you wish to run, use the command: 对于您希望运行的每个临时测试,请使用以下命令:

Testdriven.NET.AssemblyUtilities.SetEntryAssembly();

This needs to be called before content loading, such as in the first line. 这需要在内容加载之前调用,例如在第一行中。

An example of an ad hoc test can be seen here: 临时测试的示例可以在此处查看:

 public static void myAdHocTest() { Testdriven.NET.AssemblyUtilities.SetEntryAssembly(); //Do your testing here } 

Step 3. 第三步

Run the ad hoc tests like normal, by putting the caret in the test, right-click and choose “run test(s)”. 像往常一样运行临时测试,方法是将插入符号放入测试中,右键单击并选择“运行测试”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM