nsubstitute.github.io - A friendly substitute for .NET mocking libraries | NSubstitute

Example domain paragraphs

//Create: var calculator = Substitute . For < ICalculator >(); //Set a return value: calculator . Add ( 1 , 2 ). Returns ( 3 ); Assert . AreEqual ( 3 , calculator . Add ( 1 , 2 )); //Check received calls: calculator . Received (). Add ( 1 , Arg . Any < int >()); calculator . DidNotReceive (). Add ( 2 , 2 ); //Raise events calculator . PoweringUp += Raise . Event (); Helpful exceptions ReceivedCallsException : Expected to receive a call matching: Add(1, 2) Actually received no matching calls. Received 2 non-

NSubstitute is designed for Arrange-Act-Assert (AAA) testing, so you just need to arrange how it should work, then assert it received the calls you expected once you're done. Because you've got more important code to write than whether you need a mock or a stub.

There are already some great mocking libraries around for .NET, so why create another? We found that for all their great features, none of the existing libraries had the succinct syntax we were craving — the code required to configure test doubles quickly obscured the intention behind our tests.

Links to nsubstitute.github.io (4)