Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 属性跳过单元测试c中的语句#_C#_Unit Testing - Fatal编程技术网

C# 属性跳过单元测试c中的语句#

C# 属性跳过单元测试c中的语句#,c#,unit-testing,C#,Unit Testing,我希望跳过单元测试中的某个语句,例如: if (MessageBox.Show("Are you sure you want to remove " + contact.CompanyName + " from the contacts?", "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes) == MessageBoxResult.Yes) 是否可以在语句上方放置一

我希望跳过单元测试中的某个语句,例如:

if (MessageBox.Show("Are you sure you want to remove " + contact.CompanyName + " from the contacts?", "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes) == MessageBoxResult.Yes)

是否可以在语句上方放置一个属性以避免单元测试执行它?

您应该尽量避免直接调用正在进行单元测试的类中的GUI组件。解决这个问题的一种方法是创建一个接口,比如说IMessageBoxDisplayService,它可以为测试存根

public MyClass(IMessageBoxDisplayService messageBoxDisplayService)
{ 
    ...
    if (messageBoxDisplayService.Show(message)) ...

您应该尽量避免在正在进行单元测试的类中直接调用GUI组件。解决这个问题的一种方法是创建一个接口,比如说IMessageBoxDisplayService,它可以为测试存根

public MyClass(IMessageBoxDisplayService messageBoxDisplayService)
{ 
    ...
    if (messageBoxDisplayService.Show(message)) ...

您还可以尝试使用来取消对MessageBox.Show()的调用。

您还可以尝试使用来取消对MessageBox.Show()的调用。

您可能正在考虑[Conditional()]属性:


不过,我认为您最好使用Mark的接口解决方案。

您可能正在考虑[Conditional()]属性:


不过,我认为您最好使用Mark的接口解决方案。

您使用的是什么单元测试框架?您使用的是NUnit框架吗?我刚刚从visual studio 2010创建了一个新的测试项目,不确定框架您使用的是什么单元测试框架?您使用的是NUnit框架吗?我刚从visual studio 2010创建了一个新的测试项目,不确定Frameworkk听起来是否是最好的方法,但是,是否有一个属性会跳过接口中的一个语句,而不管该语句是什么?您可以使用条件属性跳过一个方法。Ok听起来是最好的方法,但是,是否有一个属性会跳过接口中的一条语句,而不管该语句是什么?您可以使用条件属性跳过一个方法。