return类型为Void的私有方法的Assert阶段怎么办?
How to do the Assert phase of a private method whose return type is Void?
我有一个 class 有一个私有方法
public class MyClass
{
private void SomeMethod(PrimaryAllocationDP packet)
{
........................
some code
........................
packet.AllocatedAgency = AgencyAllocated;
}
}
现在通过使用MSUnit测试框架,我已经写到这里了
[TestMethod]
public void TestAllocatedAgency()
{
var packet = new Fixture().Create<PrimaryAllocationDP>(); //used AutoFixture here
PrivateObject accessor = new PrivateObject(new MyClass());
accessor.Invoke("SomeMethod", packet); //Act
// what will be the Assert? Since it is void
}
Assert 是什么?既然是void,怎么写assert呢?
好吧,在示例中,被测方法正在对其 argument/dependency 进行更改,您可以断言调用该函数的预期结果是数据包的 AllocatedAgency
属性 实际上不是 null
[TestMethod]
public void TestAllocatedAgency() {
//Arrange
var packet = new Fixture().Create<PrimaryAllocationDP>(); //used AutoFixture here
var sut = new MyClass();
var accessor = new PrivateObject(sut);
//Act
accessor.Invoke("SomeMethod", packet);
//Assert
Assert.IsNotNull(packet.AllocatedAgency);
}
如果您可以更改 PrimaryAllocationDP
,您还可以添加新界面 IPrimaryAllocationDP
并测试 属性 设置。在我的测试中,我假设 AllocatedAgency
是对象类型并且我正在使用 Moq。但也许您也可以使用 AutoFixture
进行模拟?为了更清楚,我直接在 MyClass
中设置了 AgencyAllocated
[TestFixture]
public class DependencyInjection
{
[TestMethod]
public void TestAllocatedAgency()
{
var packet = new Mock<IPrimaryAllocationDP>();
PrivateObject accessor = new PrivateObject(new MyClass());
accessor.Invoke("SomeMethod", packet.Object); //Act
packet.VerifySet(p => p.AllocatedAgency = 42);
}
}
public interface IPrimaryAllocationDP
{
//object or any other type
object AllocatedAgency { set; }
}
public class PrimaryAllocationDP : IPrimaryAllocationDP
{
public object AllocatedAgency { set; private get; }
}
public class MyClass
{
private readonly object AgencyAllocated = 42;
private void SomeMethod(IPrimaryAllocationDP packet)
{
//........................
//some code
//........................
packet.AllocatedAgency = AgencyAllocated;
}
}
我有一个 class 有一个私有方法
public class MyClass
{
private void SomeMethod(PrimaryAllocationDP packet)
{
........................
some code
........................
packet.AllocatedAgency = AgencyAllocated;
}
}
现在通过使用MSUnit测试框架,我已经写到这里了
[TestMethod]
public void TestAllocatedAgency()
{
var packet = new Fixture().Create<PrimaryAllocationDP>(); //used AutoFixture here
PrivateObject accessor = new PrivateObject(new MyClass());
accessor.Invoke("SomeMethod", packet); //Act
// what will be the Assert? Since it is void
}
Assert 是什么?既然是void,怎么写assert呢?
好吧,在示例中,被测方法正在对其 argument/dependency 进行更改,您可以断言调用该函数的预期结果是数据包的 AllocatedAgency
属性 实际上不是 null
[TestMethod]
public void TestAllocatedAgency() {
//Arrange
var packet = new Fixture().Create<PrimaryAllocationDP>(); //used AutoFixture here
var sut = new MyClass();
var accessor = new PrivateObject(sut);
//Act
accessor.Invoke("SomeMethod", packet);
//Assert
Assert.IsNotNull(packet.AllocatedAgency);
}
如果您可以更改 PrimaryAllocationDP
,您还可以添加新界面 IPrimaryAllocationDP
并测试 属性 设置。在我的测试中,我假设 AllocatedAgency
是对象类型并且我正在使用 Moq。但也许您也可以使用 AutoFixture
进行模拟?为了更清楚,我直接在 MyClass
AgencyAllocated
[TestFixture]
public class DependencyInjection
{
[TestMethod]
public void TestAllocatedAgency()
{
var packet = new Mock<IPrimaryAllocationDP>();
PrivateObject accessor = new PrivateObject(new MyClass());
accessor.Invoke("SomeMethod", packet.Object); //Act
packet.VerifySet(p => p.AllocatedAgency = 42);
}
}
public interface IPrimaryAllocationDP
{
//object or any other type
object AllocatedAgency { set; }
}
public class PrimaryAllocationDP : IPrimaryAllocationDP
{
public object AllocatedAgency { set; private get; }
}
public class MyClass
{
private readonly object AgencyAllocated = 42;
private void SomeMethod(IPrimaryAllocationDP packet)
{
//........................
//some code
//........................
packet.AllocatedAgency = AgencyAllocated;
}
}