I have a simple MSpec test in which I am passing a null value into a method parameter (of type string) in a Because statement. I then check that an exception is thrown in the It statement.

Because _of = () => _exception = Catch.Exception(() => foo(null));
It should_throw_an_exception = () =>_exception.ShouldBeOfType<Exception>();

I need another test when the parameter is an empty string and the assertion should stay the same. I could write another test, duplicating code.

Or, is there a neater way to do this kind of test in MSpec?

有帮助吗?

解决方案

For things like these I would rather use NUnit's TestCaseAttribute to have all combinations of input parameters verified against the same code. MSpec just isn't the best tool for the job here, because it doesn't support test generators like the one mentioned above. Behaviors could help a bit, bit would only de-duplicate the Its. IMHO, there's nothing wrong with mixing test frameworks for things like the one you posted.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top