public void YouCanTellTheStubToThrowAnExceptionWhenAMethodIsCalled() { // Create a stub. ISampleClass stub = CreateStub(); // Calling the void method will throw exception. stub.When(s => s.VoidMethod()).Do(x => { throw new InvalidOperationException(); }); // Calling the method with "foo" as the parameter will throw exception. stub.MethodThatReturnsInteger("foo").Returns(x => { throw new InvalidOperationException(); }); // Call method that throws exception. Assert.Throws <InvalidOperationException>(stub.VoidMethod); Assert.Throws <InvalidOperationException>(() => stub.MethodThatReturnsInteger("foo")); }
public void HandlingRefParametersInStubs() { // Create a stub. ISampleClass stub = CreateStub(); // Here's how you stub an "ref" parameter. string refi = "input"; stub.When(s => s.MethodWithRefParameter(ref refi)).Do(x => x[0] = "output"); // If you call the method with the specified input argument, it will // change the parameter to the value you specified. string param = "input"; stub.MethodWithRefParameter(ref param); param.Should(Be.EqualTo("output")); // If I call the method with any other input argument, it won't // change the value. param = "some other value"; stub.MethodWithRefParameter(ref param); param.Should(Be.EqualTo("some other value")); }