English 中文(简体)
单元试验中的构造
原标题:Constructing mocks in unit tests

是否有办法建造一个 mo车,而不是在打造建筑者的测试代码时进行真正的试验?

例如:

public class ClassToTest
{
  public void MethodToTest()
  {
    MyObject foo = new MyObject();
    Console.WriteLine(foo.ToString());
  }
}

举例来说,我需要进行一项单位测试,确认在一类试验中采用“待选方法”确实将产生任何结果,而不论新创建的MyObject一案的“ToString”方法的结果如何。

我看不出一种现实的方式,孤立地测试《全面禁止核试验条约》类别;测试这一方法实际上将测试我的目标。 规范方法和方法。

最佳回答

实际情况并非如此。 您的<代码>Class To Test因其设计难以测试。 如果你想要放弃对 mo物体的依赖,那么,你需要思考如何注入这些依赖物,而只是在建筑商中制造这些依赖物并不是一种帮助你的办法。 你们可以使用建筑注射器:

public class ClassToTest
{
  MyObject _foo;
  public void MethodToTest(Myobject foo)
  {
    _foo = foo;
    Console.WriteLine(foo.ToString());
  }
}

或您可使用财产注入:

public class ClassToTest
{
  public MyObject Foo { get; set; }
  public void MethodToTest()
  {
  }
}

前者可能更适合你们的榜样,因为你想勾销那里的ole子;如果你的依赖必须是对物体的依赖,而不是选择性的,则这种做法往往是最佳的。

http://en.wikipedia.org/wiki/Dependency_injection” rel=“nofollow noretinger”>dependency injection and inversion of control

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签