9
我試圖單元測試定製ConfigurationElementCollection
,但我有編程填充集合的問題。當我撥打BaseAdd()
時,出現以下例外情況:如何編程元素添加到一個ConfigurationElementCollection中?
ConfigurationErrorsException:元素'add'已被鎖定在更高級別的配置中。
然而,運行多個測試時,這個問題纔會出現。考慮這兩個測試:
private Fixture Fixtures = new Fixture(); // AutoFixtures
[Test]
public void test1()
{
var tc = Fixtures.CreateAnonymous<TenantCollection>();
var t = Fixtures.CreateAnonymous<Tenant>();
tc.Add(t);
}
[Test]
public void test2()
{
var tc = Fixtures.CreateAnonymous<TenantCollection>();
var t = Fixtures.CreateAnonymous<Tenant>();
tc.Add(t);
}
單獨執行時,每個單獨的測試通過。當一起運行,鎖定拋出異常。
這是怎麼回事?我無論怎樣才能解開集合或工作圍繞鎖?
可以,還有,從TenantCollection實現覆蓋IsReadOnly方法並返回false –