单元测试XNA:我需要嘲弄我的GraphicsDevice我的、单元测试、XNA、GraphicsDevice

2023-09-07 08:34:27 作者:渡

我打打闹闹的XNA框架。 为了帮助我周围我做的,看起来像这样一个助手类:

I'm fooling around with the XNA framework. To help me around I made a helper class that looks like this:

ActorHolder
+ SpriteBatch (SpriteBatch)
+ ContentManager (ContentManager)
- drawables (IList<IDrawable>)
- updatables (IList<IUpdatable>)

+ ActorHolder(GraphicsDevice, ContentManager)
+ Draw(GameTime)
+ Update(GameTime)
+ AddActor(IActor)
+ RemoveActor(IActor)
+ GetCollidingActors(IActor)

现在我想单元测试这个类。但是当你看到我的构造函数需要一个图形设备和contentmanager。虽然我认为这使得SENCE在我的应用程序,它并没有在我的测试。 如果我只是为了嘲笑这两个单元测试或者是我的设计有缺陷?

Now I want to unit test this class. But as you see my constructor needs a graphics device and a contentmanager. While I think this makes sence in my application, it doesn't in my tests. Should I mock these two just in order to unit test or is my design flawed?

- UPDATE-- 我发现了一个链接到一个项目,可能会帮帮忙: HTTP://scurvytest.$c$cplex.com/ 没有与任何XP还为编码具有以腾出空间给社会生活产生了一点。

--UPDATE-- I found a link to a project that might help out: http://scurvytest.codeplex.com/ Don't have any xp with it yet as coding has to make room for social life a bit.

- Note-- 请原谅我的UML法语,我公司不使用它,所以我从来没有使用过它除了回学校。

--Note-- Excuse me my UML French, my company doesn't use it so I never used it except back at school.

推荐答案

我嘲笑的图形设备通过实际建立在一个不可见的窗口一个真实的。性能是出乎意料的好 - 12秒,约1500测试

I am "mocking" the graphics device by actually creating a real one on an invisible window. Performance is surprisingly good - 12 seconds for about 1500 tests.

这使我可以测试code这需要一个图形设备,并做一些基本的验证(例如,是正确的纹理集,已经顶点缓冲区被选中,等等)。这可以通过使用参考光栅与DirectX的调试运行时做更深入检查得到改善。

It allows me to test code which requires a graphics device and do some basic verification (eg. is the right texture set, has the vertex buffer been selected, etc.). This could be improved by using the reference rasterizer with the DirectX Debug Runtime to do more intensive checking.

如果我需要验证什么被发送到图形设备,创建通过该code-下测试可以发送顶点的接口 - 这可以很容易地嘲笑与标准的模拟对象框架

If I need to verify what gets sent to the graphics device, I create an interface through which the code-under-test can send the vertices - which can then be easily mocked with standard mock object frameworks.

检查这个问题再讨论有关嘲讽XNA图形设备相关类:嘲讽的Texture2D

Check this question for another discussion about mocking XNA graphics device dependent classes: Mocking Texture2D

下面是我使用模拟使用一个真正的图形设备类: MockedGraphicsDeviceService.cs , MockedGraphicsDeviceService.Test.cs

Here are the classes I'm using to "mock" using a real graphics device: MockedGraphicsDeviceService.cs, MockedGraphicsDeviceService.Test.cs

(编辑:固定断开的链接)

(edit: fixed broken links)