Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从单元测试调用时,参数意外初始化_C#_Unit Testing_Dependency Injection_Null_Castle Windsor - Fatal编程技术网

C# 从单元测试调用时,参数意外初始化

C# 从单元测试调用时,参数意外初始化,c#,unit-testing,dependency-injection,null,castle-windsor,C#,Unit Testing,Dependency Injection,Null,Castle Windsor,我有一个调用构造函数的单元测试,传入一个“null”来测试对null的处理 我希望调用的方法会抛出ArgumentNullException,但是当我逐步查看代码时,我看到参数实际上已经初始化 这让我感到困惑,尽管我的直觉告诉我这和DI容器(Castle Windsor)有关 有人能解释一下吗 在我的单元测试中,null与实例化委托一起传递: [Test] public void ConstructorThrowsAnExceptionWhenImplementationCollection

我有一个调用构造函数的单元测试,传入一个“null”来测试对null的处理

我希望调用的方法会抛出ArgumentNullException,但是当我逐步查看代码时,我看到参数实际上已经初始化

这让我感到困惑,尽管我的直觉告诉我这和DI容器(Castle Windsor)有关

有人能解释一下吗

在我的单元测试中,null与实例化委托一起传递:

 [Test]
 public void ConstructorThrowsAnExceptionWhenImplementationCollectionIsNull()
 {
     //assert
     Assert.Throws<ArgumentException>(() => new CacheImplementationSelector(null, _stubCacheImplementationSelectorDelegate));
 }
[测试]
public void构造函数RowsanException WhenImplementation CollectionsNull()时
{
//断言
抛出(()=>new CacheImplementationSelector(null,_stubCacheImplementationSelectorDelegate));
}
调用的方法:

public CacheImplementationSelector(ICollection<ICacheImplementation> implementations, CacheImplementationSelectorDelegate selectorDelegate)
{
    implementations.IsNotNullArgCheck("implementations");
    ...
公共缓存实现选择器(ICollection实现、缓存实现选择器或删除门选择器或删除门)
{
IsNotNullArgCheck(“实现”);
...
将鼠标悬停在implementations参数上,代码停在CacheImplementationSelectorMethod中的断点上,visual studio会告诉我参数“Implements”的计数为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);
}
[TestFixtureSetUp]
public void FixtureSetUp()
{         
_mocks=新的MockRepository();
}
[设置]
公共作废设置()
{
_ListofImplements=新列表(){u stubICacheImplementation};
_stubCacheImplementationSelectorDelegate=MockRepository.GenerateSub();
_stubICacheImplementation=MockRepository.GenerateSub();
_stubKeyCreator=MockRepository.GenerateSub();
_stubStrategy=MockRepository.GenerateStub();
_stubEncoder=MockRepository.GenerateStub();
_c=新的CacheImplementationSelector(_实现列表,_stubCacheImplementationSelectorDelegate);
_testObject=新对象();
_昨天=DateTime.Now.Subtract(新的时间跨度(1,0,0,0));
_明天=DateTime.Now.Add(newtimespan(1,0,0,0));
_testString=“test”;
_tooLongKey=“a”。重复(Cache.MaxKeyLength+1);
_重复(Cache.MaxFriendlyNameLength+1);
}

当您单步执行代码时,是否可能在[SetUp]方法中看到这一行的执行

_c = new CacheImplementationSelector(_listOfImplementations, _stubCacheImplementationSelectorDelegate);
该代码将在单元测试之前运行,“实现”方法不会为null


至于单元测试失败,我们可以看到IsNotNullArgCheck方法的实现吗?我假设它是使用某种反射的扩展方法。可能其中存在错误?

是您的测试还是您的实际应用程序的问题?我在这些测试中没有看到Windsor…为什么在单元测试中使用容器?容器是为了协调整个应用程序。单元测试是孤立地测试单个单元…如前所述,这听起来是不可能的。一定有我们缺少的某种上下文。你能描述并发布简单的代码来重现这种行为吗?我在Naive关于单元测试时问了这个问题。提供的代码不足以给出完整的答案。无法确认这一点wer是解决方案,但很可能,因此被接受。