2
我試圖使用AutoFixture
和Autofixture.Idioms
來簡化構造函數中保護條件的測試。我使用的是下面的,熟悉的,幾乎樣板,要測試的方法:接口類型自變量的保護條件的自動混合測試
[TestMethod]
public void DependencyConsumerContainsAGuardCondition ()
{
var fixture = new Fixture ();
var assertion = new GuardClauseAssertion (fixture);
assertion.Verify (typeof (DependencyConsumer).GetConstructors ());
}
我運行測試時DependencyConsumer
的構造是一個具體類型沒有問題:
public interface IDependency { }
public class ConcreteDependency : IDependency { }
public class DependencyConsumer
{
public DependencyConsumer(ConcreteDependency dependency)
{
if (dependency == null)
{
throw new ArgumentNullException ("dependency");
}
}
}
然而當,我改變構造參數IDependency
的類型,測試失敗,出現以下異常(重新格式化爲可讀性)
Test method
AutomatedTestingPlayground.Playground.DependencyConsumerContainsAGuardCondition
threw exception:
Ploeh.AutoFixture.Idioms.GuardClauseException: AutoFixture was unable to
create an instance for parameter "dependency" of method ".ctor".
Method Signature: Void .ctor(AutomatedTestingPlayground.IDependency)
Declaring Type: AutomatedTestingPlayground.DependencyConsumer
Reflected Type: AutomatedTestingPlayground.DependencyConsumer --->
Ploeh.AutoFixture.ObjectCreationException: AutoFixture was unable to
create an instance from AutomatedTestingPlayground.IDependency, most
likely because it has no public constructor, is an abstract or
non-public type.
Request path:
AutomatedTestingPlayground.IDependency
我的測試設置代碼中缺少什麼?我是否需要明確模擬IDependency
並將其作爲構造函數參數提供?鑑於AutoFixture的既定目標 - 「零摩擦TDD」 - 以及這種構造函數出現的頻率,我不確定爲什麼我需要在抽象類型時嘲笑構造函數參數,而不是當它是一個具體類型。