NUnit无法像Program一样正确调用方法。如何正确模拟呢?

时间:2019-08-14 06:15:30

标签: c# windows nunit moq

在Nunit中,我尝试测试一种方法。我准备了与生产代码中相同的输入,但是仍然无法正常执行我的工作的方法。试图模拟它,但目前没有效果。

有人告诉我,起订量是答案,这并不容易,但可以从这个论坛上向我发送“类似”问题。下面是我所能做的一切。该主题中的所有内容均已实现,但服务仍未正确调用。检查在正常情况下它是否可以正常工作(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)
    }

}

在Nunit中

 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进行测试。也许我使用的工具不对。

1 个答案:

答案 0 :(得分:0)

单元测试的思想是测试单个代码单元而不是整个系统。集成测试是您测试整个系统的地方。您应该分别测试每个类的每个公共接口。如果该类具有要从测试中排除的依赖项,则可以创建这些依赖项的模拟:这些是伪造的对象,称为 而不是调用实际代码。

例如,要测试您所说的功能不起作用,您可以编写一个调用该功能的测试,而不是编写通过其余代码来希望调用该功能的测试。您需要直接或通过模拟对象来设置测试功能所需的数据。因此,对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()上调用静态方法。另外,如果您的对象与类具有相同的名称,可能会造成混淆,惯例是类名称以大写字母开头,而实例(对象)以小写字母开头。