I working with MOQ framework for my testing. I have a scenario in which I expect a fault exception to be thrown. How can I verify it was thrown?
public void
I may be mis-reading your intent, but as far as I can see there is no need to do anything to a mock in order to test that the exception has been thrown.
It looks like you have a class with a method Foo that takes a string - lets call this InnerClass
public class InnerClass {
public virtual void Foo(string str) {
// do something with the string
}
}
and a class which contains an InnerClass as a property (someProperty) which has a member Koko that takes a List
public class OuterClass {
private readonly InnerClass someProperty;
public OuterClass(InnerClass someProperty) {
this.someProperty = someProperty;
}
public void Koko(List list) {
foreach (var str in list) {
if (str != null)
someProperty.Foo(str);
else
throw new FormatException();
}
}
}
NOTE: I cannot get List
It looks like you want to test that if you pass in a list of strings where any of them are null that a FormatException is thrown.
If so, then the only reason for a MOQ is to release us from worrying about the InnerClass functionality. Foo is a method, so, unless we are using strict mocks, we can just create an InnerClass mock with no other setup.
There is an attribute [ExpectedException] with which we can tag our test to verify that the exception has been thrown.
[TestMethod]
[ExpectedException(typeof(FormatException))]
public void ExceptionThrown() {
var list = new List() {
"Abel",
"Baker",
null,
"Charlie"
};
var outer = new OuterClass(new Mock().Object);
outer.Koko(list);
}
This test will pass if a FormatException is thrown and fail if it is not.