//ncrunch: no coverage start static void Main(string[] args) { //////////////// // Using Constructor Injection //////////////// FakeExtensionManager myFakeManager = new FakeExtensionManager(); LogAnalyzer logAn = new LogAnalyzer(myFakeManager); logAn.IsValidLogFileName("ValidFileName.SLF"); }
public void IsValidFileName_ExtManagerThrowsException_ReturnFalse() { FakeExtensionManager myFakeManager = new FakeExtensionManager(); myFakeManager.WillThrow = new Exception("This is fake"); LogAnalyzerConstructorInject log = new LogAnalyzerConstructorInject(myFakeManager); bool result = log.IsValidLogFileName("anything.anyextension"); //需要在被测方法外添加一个try-catch,并且在catch中返回false(根据需要可以对应修改catch和这个assert) Assert.False(result); }
public void OverrideTest() { //设置要使用的桩,并赋值 FakeExtensionManager stub = new FakeExtensionManager(); stub.WillBeValid = true; //创建被测试类的派生类实例 LogAnalyzerVirtualFactoryOverride logan = new LogAnalyzerVirtualFactoryOverride(stub); bool result = logan.IsValidLogFileName("file.ext"); Assert.True(result); }
public void IsValidFileName_SupportedExtension_ReturnTrue() { //设置要使用的桩,并给其赋值使其返回True FakeExtensionManager myFakeManager = new FakeExtensionManager(); myFakeManager.WillBeValid = true; ExtensionManagerFactory factory = new ExtensionManagerFactory(); factory.SetManager(myFakeManager); LogAnalyzerFactoryInject log = new LogAnalyzerFactoryInject(); bool result = log.IsValidLogFileName("anything.anyextension"); Assert.True(result); }
public void IsValidFileName_NameSupportedExtension_ReturnsTrue() { //准备一个返回true的桩 FakeExtensionManager myFakeManager = new FakeExtensionManager(); myFakeManager.WillBeValid = true; //传入桩 LogAnalyzerConstructorInject log = new LogAnalyzerConstructorInject(myFakeManager); bool result = log.IsValidLogFileName("short.ext"); Assert.True(result); }
public void IsValidFileName_SupportExtension_ReturnTrue() { //使用前面构造函数注入用的桩 FakeExtensionManager myFakeManager = new FakeExtensionManager(); myFakeManager.WillBeValid = true; LogAnalyzerPropertyInject log = new LogAnalyzerPropertyInject(); log.ExtensionManager = myFakeManager; bool result = log.IsValidLogFileName("anything.anyextension"); Assert.True(result); }