Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 125 lines (85 sloc) 5.028 kB
ced19da @dtchepak Updated README to README.markdown
dtchepak authored
1 NSubstitute
2 ========
3
d0f49d8 @dtchepak Updated README to reference Silverlight port wiki page.
dtchepak authored
4 Visit the [NSubstitute website](http://nsubstitute.github.com) for more information.
5
ced19da @dtchepak Updated README to README.markdown
dtchepak authored
6 ### What is it?
bf280ab @whatupdave Now with informativeness!
whatupdave authored
7
d0f49d8 @dtchepak Updated README to reference Silverlight port wiki page.
dtchepak authored
8 NSubstitute is designed as a friendly substitute for .NET mocking frameworks.
a22b995 @dtchepak Updating What is it? section of readme
dtchepak authored
9
d0f49d8 @dtchepak Updated README to reference Silverlight port wiki page.
dtchepak authored
10 It is an attempt to satisfy our craving for a mocking framework with a succinct syntax that helps us keep the focus on the intention of our tests, rather than on the configuration of our test doubles. We've tried to make the most frequently required operations obvious and easy to use, keeping less usual scenarios discoverable and accessible, and all the while maintaining as much natural language as possible.
a22b995 @dtchepak Updating What is it? section of readme
dtchepak authored
11
d0f49d8 @dtchepak Updated README to reference Silverlight port wiki page.
dtchepak authored
12 Perfect for those new to testing, and for others who would just like to to get their tests written with less noise and fewer lambdas.
4779f08 @dtchepak Updating NSubstitute README to provide some more information.
dtchepak authored
13
0d8dca2 @shiftkey-tester Silverlight 4.0 support
shiftkey-tester authored
14
173edf2 @dtchepak Changing rakefile to use separate config and target platform env swit…
dtchepak authored
15 ### NOTE: Framework Multi-Targeting
0d8dca2 @shiftkey-tester Silverlight 4.0 support
shiftkey-tester authored
16
d0f49d8 @dtchepak Updated README to reference Silverlight port wiki page.
dtchepak authored
17 [Brendan](https://github.com/shiftkey) is updating the build process to [support multiple framework versions](https://github.com/nsubstitute/NSubstitute/wiki/Silverlight-port), including Silverlight. See the [Silverlight port](https://github.com/nsubstitute/NSubstitute/wiki/Silverlight-port) wiki page for current status.
0d8dca2 @shiftkey-tester Silverlight 4.0 support
shiftkey-tester authored
18
ced19da @dtchepak Updated README to README.markdown
dtchepak authored
19 ### Basic use
4779f08 @dtchepak Updating NSubstitute README to provide some more information.
dtchepak authored
20
21 Let's say we have a basic calculator interface:
22
9f144ff @dtchepak Fixing README.markdown comments so they don't appear as code on github.
dtchepak authored
23 <!-- {% examplecode csharp %} -->
767a6ea @dtchepak Updating documentation
dtchepak authored
24 public interface ICalculator
25 {
26 int Add(int a, int b);
27 string Mode { get; set; }
28 event Action PoweringUp;
29 }
9f144ff @dtchepak Fixing README.markdown comments so they don't appear as code on github.
dtchepak authored
30 <!-- {% endexamplecode %} -->
31 <!-- {% requiredcode %}
4fee616 @dtchepak Docs: checking code in README.markdown
dtchepak authored
32 ICalculator _calculator;
33 [SetUp]
34 public void SetUp() { _calculator = Substitute.For<ICalculator>(); }
9f144ff @dtchepak Fixing README.markdown comments so they don't appear as code on github.
dtchepak authored
35 {% endrequiredcode %} -->
767a6ea @dtchepak Updating documentation
dtchepak authored
36
37 We can ask NSubstitute to create a substitute instance for this type. We could ask for a stub, mock, fake, spy, test double etc., but why bother when we just want to substitute an instance we have some control over?
38
9f144ff @dtchepak Fixing README.markdown comments so they don't appear as code on github.
dtchepak authored
39 <!-- {% examplecode csharp %} -->
767a6ea @dtchepak Updating documentation
dtchepak authored
40 _calculator = Substitute.For<ICalculator>();
9f144ff @dtchepak Fixing README.markdown comments so they don't appear as code on github.
dtchepak authored
41 <!-- {% endexamplecode %} -->
767a6ea @dtchepak Updating documentation
dtchepak authored
42
43 Now we can tell our substitute to return a value for a call:
44
9f144ff @dtchepak Fixing README.markdown comments so they don't appear as code on github.
dtchepak authored
45 <!-- {% examplecode csharp %} -->
767a6ea @dtchepak Updating documentation
dtchepak authored
46 _calculator.Add(1, 2).Returns(3);
47 Assert.That(_calculator.Add(1, 2), Is.EqualTo(3));
9f144ff @dtchepak Fixing README.markdown comments so they don't appear as code on github.
dtchepak authored
48 <!-- {% endexamplecode %} -->
767a6ea @dtchepak Updating documentation
dtchepak authored
49
50 We can check that our substitute received a call, and did not receive others:
51
9f144ff @dtchepak Fixing README.markdown comments so they don't appear as code on github.
dtchepak authored
52 <!-- {% examplecode csharp %} -->
767a6ea @dtchepak Updating documentation
dtchepak authored
53 _calculator.Add(1, 2);
54 _calculator.Received().Add(1, 2);
55 _calculator.DidNotReceive().Add(5, 7);
9f144ff @dtchepak Fixing README.markdown comments so they don't appear as code on github.
dtchepak authored
56 <!-- {% endexamplecode %} -->
767a6ea @dtchepak Updating documentation
dtchepak authored
57
58 If our Received() assertion fails, NSubstitute tries to give us some help as to what the problem might be:
59
8446ebd @dtchepak Updating sample example message in README.
dtchepak authored
60
61 NSubstitute.Exceptions.ReceivedCallsException : Expected to receive a call matching:
4fee616 @dtchepak Docs: checking code in README.markdown
dtchepak authored
62 Add(1, 2)
8446ebd @dtchepak Updating sample example message in README.
dtchepak authored
63 Actually received no matching calls.
64 Received 2 non-matching calls (non-matching arguments indicated with '*' characters):
4fee616 @dtchepak Docs: checking code in README.markdown
dtchepak authored
65 Add(1, *5*)
66 Add(*4*, *7*)
767a6ea @dtchepak Updating documentation
dtchepak authored
67
68 We can also work with properties using the Returns syntax we use for methods, or just stick with plain old property setters (for read/write properties):
69
9f144ff @dtchepak Fixing README.markdown comments so they don't appear as code on github.
dtchepak authored
70 <!-- {% examplecode csharp %} -->
767a6ea @dtchepak Updating documentation
dtchepak authored
71 _calculator.Mode.Returns("DEC");
72 Assert.That(_calculator.Mode, Is.EqualTo("DEC"));
d3eed6c @dtchepak Removing Raise.Action methods and Raise.EventWithEmptyEventArgs (use …
dtchepak authored
73
767a6ea @dtchepak Updating documentation
dtchepak authored
74 _calculator.Mode = "HEX";
75 Assert.That(_calculator.Mode, Is.EqualTo("HEX"));
9f144ff @dtchepak Fixing README.markdown comments so they don't appear as code on github.
dtchepak authored
76 <!-- {% endexamplecode %} -->
767a6ea @dtchepak Updating documentation
dtchepak authored
77
78 NSubstitute supports argument matching for setting return values and asserting a call was received:
79
9f144ff @dtchepak Fixing README.markdown comments so they don't appear as code on github.
dtchepak authored
80 <!-- {% examplecode csharp %} -->
767a6ea @dtchepak Updating documentation
dtchepak authored
81 _calculator.Add(10, -5);
6e5c1bb @dtchepak Update for 0.1.2 alpha 3 release
dtchepak authored
82 _calculator.Received().Add(10, Arg.Any<int>());
83 _calculator.Received().Add(10, Arg.Is<int>(x => x < 0));
9f144ff @dtchepak Fixing README.markdown comments so they don't appear as code on github.
dtchepak authored
84 <!-- {% endexamplecode %} -->
767a6ea @dtchepak Updating documentation
dtchepak authored
85
86 We can use argument matching as well as passing a function to Returns() to get some more behaviour out of our substitute (possibly too much, but that's your call):
87
9f144ff @dtchepak Fixing README.markdown comments so they don't appear as code on github.
dtchepak authored
88 <!-- {% examplecode csharp %} -->
767a6ea @dtchepak Updating documentation
dtchepak authored
89 _calculator
90 .Add(Arg.Any<int>(), Arg.Any<int>())
91 .Returns(x => (int)x[0] + (int)x[1]);
92 Assert.That(_calculator.Add(5, 10), Is.EqualTo(15));
9f144ff @dtchepak Fixing README.markdown comments so they don't appear as code on github.
dtchepak authored
93 <!-- {% endexamplecode %} -->
767a6ea @dtchepak Updating documentation
dtchepak authored
94
95 Returns() can also be called with multiple arguments to set up a sequence of return values.
96
9f144ff @dtchepak Fixing README.markdown comments so they don't appear as code on github.
dtchepak authored
97 <!-- {% examplecode csharp %} -->
767a6ea @dtchepak Updating documentation
dtchepak authored
98 _calculator.Mode.Returns("HEX", "DEC", "BIN");
99 Assert.That(_calculator.Mode, Is.EqualTo("HEX"));
100 Assert.That(_calculator.Mode, Is.EqualTo("DEC"));
101 Assert.That(_calculator.Mode, Is.EqualTo("BIN"));
9f144ff @dtchepak Fixing README.markdown comments so they don't appear as code on github.
dtchepak authored
102 <!-- {% endexamplecode %} -->
767a6ea @dtchepak Updating documentation
dtchepak authored
103
104 Finally, we can raise events on our substitutes (unfortunately C# dramatically restricts the extent to which this syntax can be cleaned up):
105
9f144ff @dtchepak Fixing README.markdown comments so they don't appear as code on github.
dtchepak authored
106 <!-- {% examplecode csharp %} -->
767a6ea @dtchepak Updating documentation
dtchepak authored
107 bool eventWasRaised = false;
108 _calculator.PoweringUp += () => eventWasRaised = true;
d3eed6c @dtchepak Removing Raise.Action methods and Raise.EventWithEmptyEventArgs (use …
dtchepak authored
109
110 _calculator.PoweringUp += Raise.Event<Action>();
767a6ea @dtchepak Updating documentation
dtchepak authored
111 Assert.That(eventWasRaised);
9f144ff @dtchepak Fixing README.markdown comments so they don't appear as code on github.
dtchepak authored
112 <!-- {% endexamplecode %} -->
4779f08 @dtchepak Updating NSubstitute README to provide some more information.
dtchepak authored
113
ced19da @dtchepak Updated README to README.markdown
dtchepak authored
114 ### Building
4779f08 @dtchepak Updating NSubstitute README to provide some more information.
dtchepak authored
115
c78155a @dtchepak Updated readme to mention you can do a simple compile and unit test r…
dtchepak authored
116 If you have Visual Studio 2008 or 2010 you should be able to compile NSubstitute and run the unit tests using the NUnit GUI or console test runner (see the ThirdParty directory).
117 To do full builds you'll also need Ruby and rake to run the rakefile.
4779f08 @dtchepak Updating NSubstitute README to provide some more information.
dtchepak authored
118
1d11c58 @dtchepak Adding discussion group to documentation
dtchepak authored
119 ### Getting help
120
121 If you have questions or feedback on NSubstitute, head on over to the [NSubstitute discussion group](http://groups.google.com/group/nsubstitute).
122
123
124
Something went wrong with that request. Please try again.