从单元测试调用时参数意外初始化
我有一个调用构造函数的单元测试,故意传入“null”来测试对 null 的处理。
我希望调用的方法会抛出 ArgumentNullException,但是当我单步执行代码时,我看到参数实际上已初始化。
这让我很困惑,尽管我的直觉告诉我这与 DI 容器(温莎城堡)有关。
任何人都可以阐明这一点吗?
我的单元测试,空值与实例化委托一起传递:
[Test]
public void ConstructorThrowsAnExceptionWhenImplementationCollectionIsNull()
{
//assert
Assert.Throws<ArgumentException>(() => new CacheImplementationSelector(null, _stubCacheImplementationSelectorDelegate));
}
调用的方法:
public CacheImplementationSelector(ICollection<ICacheImplementation> implementations, CacheImplementationSelectorDelegate selectorDelegate)
{
implementations.IsNotNullArgCheck("implementations");
...
将鼠标悬停在实现参数上,代码停在 CacheImplementationSelectorMethod 中的断点上,Visual Studio 告诉我参数“实现”的计数为 1并且 [0] 为空。
我正在使用 ReSharper 运行 NUnit 测试。
为了完整起见,TestFixtureSetup 和 SetUp 如下:
[TestFixtureSetUp]
public void FixtureSetUp()
{
_mocks = new MockRepository();
}
[SetUp]
public void Setup()
{
_listOfImplementations = new List<ICacheImplementation>() { _stubICacheImplementation };
_stubCacheImplementationSelectorDelegate = MockRepository.GenerateStub<CacheImplementationSelectorDelegate>();
_stubICacheImplementation = MockRepository.GenerateStub<ICacheImplementation>();
_stubKeyCreator = MockRepository.GenerateStub<ICacheKeyCreator>();
_stubStrategy = MockRepository.GenerateStub<ICachingStrategy>();
_stubEncoder = MockRepository.GenerateStub<ICacheItemEncoder>();
_c = new CacheImplementationSelector(_listOfImplementations, _stubCacheImplementationSelectorDelegate);
_testObject = new object();
_yesterday = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0));
_tomorrow = DateTime.Now.Add(new TimeSpan(1, 0, 0, 0));
_testString = "test";
_tooLongKey = "a".Repeat(Cache.MaxKeyLength+1);
_tooLongFriendlyName = "a".Repeat(Cache.MaxFriendlyNameLength + 1);
}
I have a unit test invoking a constructor, passing in a "null" on purpose to test the handling of the null.
I expect the method invoked to throw an ArgumentNullException, but when I step through the code, I see the parameter has actually been initialised.
This has me stumped, although my gut says it has something to do with the DI container (Castle Windsor).
Can anyone shed any light on this?
My unit test, a null is passed together with an instantiated delegate:
[Test]
public void ConstructorThrowsAnExceptionWhenImplementationCollectionIsNull()
{
//assert
Assert.Throws<ArgumentException>(() => new CacheImplementationSelector(null, _stubCacheImplementationSelectorDelegate));
}
The invoked method:
public CacheImplementationSelector(ICollection<ICacheImplementation> implementations, CacheImplementationSelectorDelegate selectorDelegate)
{
implementations.IsNotNullArgCheck("implementations");
...
Hovering my mouse over the implementations parameter with the code stopped on a breakpoint in the CacheImplementationSelectorMethod, visual studio tells me the parameter "implementations" has a Count of 1 and [0] is null.
I am using ReSharper to run the NUnit test.
For completeness the TestFixtureSetup and SetUp are as follows:
[TestFixtureSetUp]
public void FixtureSetUp()
{
_mocks = new MockRepository();
}
[SetUp]
public void Setup()
{
_listOfImplementations = new List<ICacheImplementation>() { _stubICacheImplementation };
_stubCacheImplementationSelectorDelegate = MockRepository.GenerateStub<CacheImplementationSelectorDelegate>();
_stubICacheImplementation = MockRepository.GenerateStub<ICacheImplementation>();
_stubKeyCreator = MockRepository.GenerateStub<ICacheKeyCreator>();
_stubStrategy = MockRepository.GenerateStub<ICachingStrategy>();
_stubEncoder = MockRepository.GenerateStub<ICacheItemEncoder>();
_c = new CacheImplementationSelector(_listOfImplementations, _stubCacheImplementationSelectorDelegate);
_testObject = new object();
_yesterday = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0));
_tomorrow = DateTime.Now.Add(new TimeSpan(1, 0, 0, 0));
_testString = "test";
_tooLongKey = "a".Repeat(Cache.MaxKeyLength+1);
_tooLongFriendlyName = "a".Repeat(Cache.MaxFriendlyNameLength + 1);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您单步执行代码时,是否有可能在 [SetUp] 方法中看到这一行的执行?
该代码将在单元测试之前运行,并且“实现”方法不会为空。
至于单元测试失败,我们可以看看IsNotNullArgCheck方法的实现吗?我认为这是使用一些反射的某种扩展方法。也许那里有一个错误?
Is it possible that when you are stepping through the code you are seeing the execution of this line in the [SetUp] method?
That code would run before your unit test and the "implementation" method would not be null.
As for the unit test failing, can we see the implementation of the IsNotNullArgCheck method? I assume it's some extension method using some reflection. Maybe there is a bug in there?