NUnit:如何在C#中使用"ref"参数测试私有方法

Nik*_*rma 3 c# nunit

我有一个私有方法,如下所示:

int void SomeMethod(ref string theStr)
{
   // Some Implementation
}
Run Code Online (Sandbox Code Playgroud)

如何为这种方法编写单元测试用例.

Igo*_*aka 7

似乎有点没有意义,方法是无效的,但需要一个ref参数.使它返回一个字符串可能是有意义的:

public class FooBar {
 internal string SomeMethod(ref string theStr) { 
    // Some Implementation 
    return theStr;
 }
}
Run Code Online (Sandbox Code Playgroud)

我们还在AssemblyInfo.cs文件中创建它internal并指定InternalVisibleTo属性:

 [assembly: InternalsVisibleTo("Test.Assembly")]
Run Code Online (Sandbox Code Playgroud)

这种方式SomeMethod将表现为内部(即在其组件外部不可见),除了Test.Assembly,它将看作它public.

单元测试非常简单(无论是否需要ref参数).

[Test]
public void SomeMethodShouldReturnSomething() { 
   Foobar foobar = new Foobar();
   string actual;
   foobar.SomeMethod(ref actual);
   Assert.AreEqual("I'm the test your tests could smell like", actual);
}
Run Code Online (Sandbox Code Playgroud)