我是单元测试的新手,感觉我在这里遗漏了一些非常重要的东西。我想在下面测试 DoSomethingWithArray 的结果:
class Traffic:ITraffic
{
private HugeArray _hugeArray;
public Traffic(HugeArray hugeArray)
{
_hugeArray = hugeArray;
}
public int DoSomethingWithArray()
{
var ret = 0;
//Here some code that does something with big array and stores some integer values in ret
return ret;
}
}
class HugeArray
{
//This is my custom data structure;
}
interface ITraffic
{
int DoSomethingWithArray();
}
我正在使用 Nunit,从我读到的内容来看,模拟接口比模拟类更好。我的问题是,我想测试的是 DosomethingWithArray 在 Traffic 类中的具体功能,我很难概念化 ITraffic 接口如何适应。我在这里缺少什么?
编辑这是我将如何测试我的课程
[TestFixture]
public class TrafficTests
{
private Traffic _traffic;
private const int size = 1000000;
private const int key = 1851925790;
[OneTimeSetUp]
public void Setup()
{
var hugeArray = new HugeArray(size);
//Some Setups to create an edge case, not relevant to my question
hugeArray.AddValue(size - 1, Int.MaxValue);
hugeArray.AddValue(size - 2, key);
//This is the object I want to test,
_traffic = new Traffic(hugeArray);
}
[Test]
public void DoSomethingWithArray_Test()
{
Assert.DoesNotThrow(() =>
{
var ret = _traffic.DoSomethingWithArray();
Assert.AreEqual(ret, 233398);
});
}
}
我的问题是:这种方法看起来正确吗?为测试创建的对象是否正常,或者我应该模拟 ITraffic 接口吗?
白衣染霜花
相关分类