public void Returns_ArgAny_Throws() { IFileNameRules fakeRules = Substitute.For <IFileNameRules>(); fakeRules.When(x => x.IsValidLogFileName(Arg.Any <string>())).Do(context => { throw new Exception("sztuczny wyjątek"); }); Assert.Throws <Exception>(() => fakeRules.IsValidLogFileName("cokolwiek")); }
public void Returns_ArgAny_Throws() { IFileNameRules fakeRules = Substitute.For <IFileNameRules>(); fakeRules.When(x => x.IsValidLogFileName(Arg.Any <string>())) .Do(a => { throw new Exception("fake exception"); }); Assert.Throws <Exception>(() => fakeRules.IsValidLogFileName("anything")); }
public void Analyze_FileNameTooShort_CallLogger_Exception() { IFileNameRules rule = Substitute.For <IFileNameRules>(); rule.When(x => x.IsValidLogFileName(Arg.Any <string>())) .Do(t => { throw new Exception("fake exception"); }); var analyzer = new LogAnalyzer(rule); Assert.Throws <Exception>(() => { analyzer.IsValidFileName("a"); }); }
public void Returns_ArgAny_Throws() { IFileNameRules fakeRules = Substitute.For <IFileNameRules>(); //When方法必须使用Lambda表达式,其中x代表要改变行为的伪对象,context包含调用的参数值 fakeRules.When(x => x.IsValidLogFileName(Arg.Any <string>())) .Do(context => { throw new Exception("fake exception"); }); Assert.Throws <Exception>(() => fakeRules.IsValidLogFileName("anything")); }
public void Returns_ArgAny_Throws() { IFileNameRules fakeRules = Substitute.For <IFileNameRules>(); fakeRules.When(x => x.IsValidLogFileName(Arg.Any <string>())) // x signifies fake object behaviour is changed on .Do(context => // context is callinfo property from NSub holding argument values at runtime. // allows cool things to be done in more advanced scenarios { throw new Exception("fake exception"); }); Assert.Throws <Exception>(() => fakeRules.IsValidLogFileName("anything")); }