使用VS2010进行单元测试

发布于 2024-12-20 22:04:10 字数 586 浏览 1 评论 0原文

我正在对一个方法进行单元测试,比如 ABC()

myclass target = new myclass();
target.ABC();

这个 ABC() 方法依次调用来自不同类的另一个方法 XYZ(),例如 anotherclass.XYZ() 和这个XYZ() 方法的输入参数取决于文本框中的值。

由于在测试运行时没有传递文本框中的值,因此我在运行测试时收到空引用异常(我的意思是测试因异常而失败)。

类似的情况(另一个):方法从查询字符串中获取 id 值,

HttpContext context
id = context.request.querystring["id"]

因为,在测试运行时,此 id 值为空,我得到一个空引用异常(我的意思是测试因异常而失败)。

据我了解,逻辑上这是正确的,因为这是测试运行而不是实际运行,但仍然想确认一次......

我做错了什么吗?

或者

我的测试运行正常吗?

请建议。感谢您的帮助。

谢谢。

I am unit testing a method say ABC() like

myclass target = new myclass();
target.ABC();

This ABC() method in turn calls another method XYZ() from a different class like anotherclass.XYZ() and this XYZ() method's input parameter depends on values from textbox (s).

Since, at test run there is no values from textbox passed I am getting a null referrence exception while running the test (I mean test fails with exception).

Similar situation (another): method fetches id value from querystring like

HttpContext context
id = context.request.querystring["id"]

Since, at test run this id values is null I get a null referrence exception(I mean test fails with exception).

To my understanding, logically this is correct since this is test run and not actual run but still want to confirm once ...

Am I doing anything wrong?

OR

My test is functioning correct?

Please suggest. Appreciate your help.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恋你朝朝暮暮 2024-12-27 22:04:10

我不知道你的代码是什么样的,但它可能需要稍微重构一下以允许单元测试。

例如,如果您有这样的代码:

MyClass{

  private AnotherClass _another = new AnotherClass();

  public string ABC(){
    return _another.XYZ();
  }
}

public AnotherClass{
  //Context comes from somewhere...
  public string XYZ(){
    return context.request.querystring["id"]
  }
}

您可能希望将其更改为这样的内容:

interface IAnotherClass{
  string XYZ();
}

MyClass{  
  private IAnotherClass _another = new AnotherClass();

  public MyClass(IAnotherClass anotherClass){
    _another = anotherClass;
  }

  public string ABC(){
    return _another.XYZ();
  }
}

public AnotherClass: IAnotherClass{
  //Context comes from somewhere...
  public string XYZ(){
    return context.request.querystring["id"]
  }
}

然后您可以使用模拟框架来模拟 IAnotherClass,如 IceN 所示。至于 HttpContext - 这变得有点复杂,但如果它仅由 AnotherClass 使用,您将不需要模拟它,因为模拟对象只会返回您喜欢的任何内容。显然,当您测试 AnothrClass 时,它就成为一个问题。如果您发现确实需要模拟它,那么有很多示例可以说明该怎么做 - 例如 这个

I have no idea what your code looks like but it probably needs to be refactored a little bit to allow unit testing.

For instance, if you have code like this:

MyClass{

  private AnotherClass _another = new AnotherClass();

  public string ABC(){
    return _another.XYZ();
  }
}

public AnotherClass{
  //Context comes from somewhere...
  public string XYZ(){
    return context.request.querystring["id"]
  }
}

You would want to change it to something like this:

interface IAnotherClass{
  string XYZ();
}

MyClass{  
  private IAnotherClass _another = new AnotherClass();

  public MyClass(IAnotherClass anotherClass){
    _another = anotherClass;
  }

  public string ABC(){
    return _another.XYZ();
  }
}

public AnotherClass: IAnotherClass{
  //Context comes from somewhere...
  public string XYZ(){
    return context.request.querystring["id"]
  }
}

You can then use a mocking framework to mock up the IAnotherClass as IceN has show. As for the HttpContext - that becomes a little more complicated, but if it's only used by the AnotherClass you wont need to mock it since the mock object will just return whatever you like. Obviously when you come to test AnotehrClass it becomes an issue. If you find you do need to mock it then there are a number of examples of what to do - for instance this one.

糖粟与秋泊 2024-12-27 22:04:10

您需要使用 anotherclass 的“非真实”实例,而是使用假 XYZ 实现的模拟实例。

我将用 Moq 框架做一个例子。

Mock<IAnotherclass> anotherClassMock= new Mock<IAnotherclass>();//create mock instanse
anotherClassMock.Setup(x=>x.XYZ);//add .Returns(something) if method XYZ returns some value
myclass target = new myclass(anotherClassMock.Object);//create instance of object under test with fake instance of anotherClassMock
target.ABC();
anotherClassMock.Verify(x=>x.XYZ);//verify that method XYZ is actualy been called

首先,您需要提取类 anotherclass 的接口才能创建 Mock。在我的示例中,我使用了一个接口 IAnotherclass

为什么我们要这样做?我们不干涉anotherclass类的逻辑。我们假设它是正确的。因此,我们从中进行抽象并仅测试 myclass 的逻辑。让我们只验证 XYZ 方法已被调用。

You need to use "not real" instance of anotherclass, but Mocked instance with fake XYZ implementation.

I'll make an example with Moq framework.

Mock<IAnotherclass> anotherClassMock= new Mock<IAnotherclass>();//create mock instanse
anotherClassMock.Setup(x=>x.XYZ);//add .Returns(something) if method XYZ returns some value
myclass target = new myclass(anotherClassMock.Object);//create instance of object under test with fake instance of anotherClassMock
target.ABC();
anotherClassMock.Verify(x=>x.XYZ);//verify that method XYZ is actualy been called

First of all you need to extract an interface of class anotherclass in order to create Mock. In my example I used an interface IAnotherclass.

Why we are doing like that? We do not intresded in logic of class anotherclass. We assume it is correct. So we abstracting from it and testing only logic of myclass. And leter we only veryfy that method XYZ is has been called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文