NUnit 无法像 Program 那样调用正确的方法。如何正确模拟它?
NUnit cannot call properly method as Program can. How to Mock it properly?
在 Nunit 中,我尝试测试一种方法。我准备了与生产代码相同的输入,但仍然无法正确调用执行我的工作的方法。试过模拟一下,暂时没有效果。
有人告诉我最小起订量是答案,这并不容易,但请从该论坛向我发送 "Similar" 问题。下面是我所能做的一切尝试。该主题中的所有内容均已实施,但仍未正确调用服务。检查在正常情况下它是否可以工作 (program.cs) 并且函数被调用,并且可以正常工作。
public class Helper
{
public string a;
public string b;
public Helper(string aa, string bb)
{
a = aa;
b = bb; //here is some transformation, but I checked it, and it's working properly
}
}
public class Service
{
public static string NotWorkingFunction(Helper o)
{
InternallService w = ThatPrivateFunctionWorks(o);
return ThatPrivateFunctionDont(w);
}
private InternallService ThatPrivateFunctionWorks(Helper o)
{
return DLL_external.SomeInternalService(o); //call was ok in both program, and in NUnit
}
ThatPrivateFunctionDont(InternallService w)
{
return DLL_external.CallingServiceFarAwayFromDLL(w); //this one works if is in part of program, but does not from NUnit. checks if have permission from Windows Credentials, then do a work. Error here from NUnit is that he cannot even call this function!
}
}
public class InternallService
{
public string smth;
public InternallService(Helper o)
{
smth = o.a;
}
}
public class DLL_external
{
public InternallService SomeInternalService(Helper o)
{
InternallService p = new InternallService(o);
return p; //prepare service for function. does not need to connect. output also is checked n another way, and is ok.
}
public InternallService CallingServiceFarAwayFromDLL(InternallService o)
{
return o; //here it connects to the service (don't know how, which protocol etc. works if run under program.cs)
}
}
在尼尼特
public class Test
{
[Test]
public void Tester()
{
Mock<Helper> MockedObject = new Mock<Helper>("a", "B"); //Mocking an object
Mock<Service> MockedService = new Mock<Service>(MockBehavior.Strict);
var Helper = new Helper("a", "B");
Service.NotWorkingFunction(MockedObject.Object); //still does not work properly. cannot call service inside the function (But very similar thing Can, but in Program.cs)
MockedService.Object.NotWorkingFunction(MockedObject.Object);//service does not call
MockedService.Setup(p => p.NotWorkingFunction(MockedObject.Object)); //found at Stack overflow, but still function is not called
//of course all uncompiling things are commented in mine code
}
}
预计工作。但仍然不知道如何调用它来从 NUnit 进行测试。也许我使用的工具有误。
单元测试的理念是测试单个代码单元而不是整个系统。集成测试是测试整个系统的地方。您应该单独测试每个 class 的每个 public 接口。如果 class 具有您想要从测试中排除的依赖项,您可以创建这些依赖项的模拟:这些是被调用的假对象 而不是 调用您的实际代码。
例如,要测试您所说的不起作用的函数,您需要编写调用该函数的测试,而不是通过其余代码来希望调用该函数的测试。您需要直接或使用模拟对象设置被测试函数所需的数据。所以 Dll_External 的测试可能会像这样开始:
[TestFixture]
public class Dll_External_Tests
{
[Test]
public void ShouldReturnAnInternalServiceFromCallingServiceFarAwayFromDLL()
{
// setup
Helper helper = new Helper("a", "B");
InternallService internalService = new InternallService(helper);
DLL_external dLL_external = new DLL_external();
// act
var result = dLL_external.CallingServiceFarAwayFromDLL(internalService);
// assert
Assert.IsNotNull(result);
Assert.IsTrue(result is InternallService);
// add more assertions for what you expect the result to be
}
}
您会看到此测试根本不使用 Service
- 它仅测试 DLL_External 并且仅创建它需要能够执行此操作的对象。
顺便说一句,您显示的代码无法编译:您需要一个对象来调用方法。您只能像 ClassName.StaticMethod()
一样直接在 class 上调用静态方法。此外,如果您的对象与 class 同名,可能会有点混乱,惯例是 class 名称以大写字母开头,实例(对象)以小写字母开头。
在 Nunit 中,我尝试测试一种方法。我准备了与生产代码相同的输入,但仍然无法正确调用执行我的工作的方法。试过模拟一下,暂时没有效果。
有人告诉我最小起订量是答案,这并不容易,但请从该论坛向我发送 "Similar" 问题。下面是我所能做的一切尝试。该主题中的所有内容均已实施,但仍未正确调用服务。检查在正常情况下它是否可以工作 (program.cs) 并且函数被调用,并且可以正常工作。
public class Helper
{
public string a;
public string b;
public Helper(string aa, string bb)
{
a = aa;
b = bb; //here is some transformation, but I checked it, and it's working properly
}
}
public class Service
{
public static string NotWorkingFunction(Helper o)
{
InternallService w = ThatPrivateFunctionWorks(o);
return ThatPrivateFunctionDont(w);
}
private InternallService ThatPrivateFunctionWorks(Helper o)
{
return DLL_external.SomeInternalService(o); //call was ok in both program, and in NUnit
}
ThatPrivateFunctionDont(InternallService w)
{
return DLL_external.CallingServiceFarAwayFromDLL(w); //this one works if is in part of program, but does not from NUnit. checks if have permission from Windows Credentials, then do a work. Error here from NUnit is that he cannot even call this function!
}
}
public class InternallService
{
public string smth;
public InternallService(Helper o)
{
smth = o.a;
}
}
public class DLL_external
{
public InternallService SomeInternalService(Helper o)
{
InternallService p = new InternallService(o);
return p; //prepare service for function. does not need to connect. output also is checked n another way, and is ok.
}
public InternallService CallingServiceFarAwayFromDLL(InternallService o)
{
return o; //here it connects to the service (don't know how, which protocol etc. works if run under program.cs)
}
}
在尼尼特
public class Test
{
[Test]
public void Tester()
{
Mock<Helper> MockedObject = new Mock<Helper>("a", "B"); //Mocking an object
Mock<Service> MockedService = new Mock<Service>(MockBehavior.Strict);
var Helper = new Helper("a", "B");
Service.NotWorkingFunction(MockedObject.Object); //still does not work properly. cannot call service inside the function (But very similar thing Can, but in Program.cs)
MockedService.Object.NotWorkingFunction(MockedObject.Object);//service does not call
MockedService.Setup(p => p.NotWorkingFunction(MockedObject.Object)); //found at Stack overflow, but still function is not called
//of course all uncompiling things are commented in mine code
}
}
预计工作。但仍然不知道如何调用它来从 NUnit 进行测试。也许我使用的工具有误。
单元测试的理念是测试单个代码单元而不是整个系统。集成测试是测试整个系统的地方。您应该单独测试每个 class 的每个 public 接口。如果 class 具有您想要从测试中排除的依赖项,您可以创建这些依赖项的模拟:这些是被调用的假对象 而不是 调用您的实际代码。
例如,要测试您所说的不起作用的函数,您需要编写调用该函数的测试,而不是通过其余代码来希望调用该函数的测试。您需要直接或使用模拟对象设置被测试函数所需的数据。所以 Dll_External 的测试可能会像这样开始:
[TestFixture]
public class Dll_External_Tests
{
[Test]
public void ShouldReturnAnInternalServiceFromCallingServiceFarAwayFromDLL()
{
// setup
Helper helper = new Helper("a", "B");
InternallService internalService = new InternallService(helper);
DLL_external dLL_external = new DLL_external();
// act
var result = dLL_external.CallingServiceFarAwayFromDLL(internalService);
// assert
Assert.IsNotNull(result);
Assert.IsTrue(result is InternallService);
// add more assertions for what you expect the result to be
}
}
您会看到此测试根本不使用 Service
- 它仅测试 DLL_External 并且仅创建它需要能够执行此操作的对象。
顺便说一句,您显示的代码无法编译:您需要一个对象来调用方法。您只能像 ClassName.StaticMethod()
一样直接在 class 上调用静态方法。此外,如果您的对象与 class 同名,可能会有点混乱,惯例是 class 名称以大写字母开头,实例(对象)以小写字母开头。