Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 119 lines (88 sloc) 3.724 kb
2edcb8a @timscott Added README. Ported from Codeplex.
timscott authored
1 ## Project Description
2
3 The **Should Assertion Library** provides a set of extension methods for test assertions for AAA and BDD style tests. It provides assertions only, and as a result it is Test runner agnostic. The assertions are a direct fork of the [xUnit](http://xunit.codeplex.com) test assertions. This project was born because test runners *Should* be independent of the the assertions!
4
5 **Install should from nuget**
6
7 PM> install-package should
8
404eebb @timscott Fixed error in readme.
timscott authored
9 The following example shows some of the assertions that are available for objects, booleans, string, and collections.
2edcb8a @timscott Added README. Ported from Codeplex.
timscott authored
10
11 public void Should_assertions()
12 {
13 object obj = null;
14 obj.ShouldBeNull();
15
16 obj = new object();
17 obj.ShouldBeType(typeof(object));
18 obj.ShouldEqual(obj);
19 obj.ShouldNotBeNull();
20 obj.ShouldNotBeSameAs(new object());
21 obj.ShouldNotBeType(typeof(string));
22 obj.ShouldNotEqual("foo");
23
24 obj = "x";
25 obj.ShouldNotBeInRange("y", "z");
26 obj.ShouldBeInRange("a", "z");
27 obj.ShouldBeSameAs("x");
28
29 "This String".ShouldContain("This");
30 "This String".ShouldNotBeEmpty();
31 "This String".ShouldNotContain("foobar");
32
33 false.ShouldBeFalse();
34 true.ShouldBeTrue();
35
36 var list = new List<object>();
37 list.ShouldBeEmpty();
38 list.ShouldNotContain(new object());
39
40 var item = new object();
41 list.Add(item);
42 list.ShouldNotBeEmpty();
43 list.ShouldContain(item);
44 }
45
46
47 Should also provides a fluent API. Here are the same assertions as above using the fluent API. Should.Fluent is a direct port of [ShouldIt](http://code.google.com/p/shouldit).
48
49 **Install should from nuget**
50
f87bc89 @timscott Fixed error in readme.
timscott authored
51 PhM> install-package ShouldFluent
2edcb8a @timscott Added README. Ported from Codeplex.
timscott authored
52
404eebb @timscott Fixed error in readme.
timscott authored
53 The following shows the same assertions as above but in the fluent style.
54
2edcb8a @timscott Added README. Ported from Codeplex.
timscott authored
55 public void Should_fluent_assertions()
56 {
57 object obj = null;
58 obj.Should().Be.Null();
59
60 obj = new object();
61 obj.Should().Be.OfType(typeof(object));
62 obj.Should().Equal(obj);
63 obj.Should().Not.Be.Null();
64 obj.Should().Not.Be.SameAs(new object());
65 obj.Should().Not.Be.OfType<string>();
66 obj.Should().Not.Equal("foo");
67
68 obj = "x";
69 obj.Should().Not.Be.InRange("y", "z");
70 obj.Should().Be.InRange("a", "z");
71 obj.Should().Be.SameAs("x");
72
73 "This String".Should().Contain("This");
74 "This String".Should().Not.Be.Empty();
75 "This String".Should().Not.Contain("foobar");
76
77 false.Should().Be.False();
78 true.Should().Be.True();
79
80 var list = new List<object>();
81 list.Should().Count.Zero();
82 list.Should().Not.Contain.Item(new object());
83
84 var item = new object();
85 list.Add(item);
86 list.Should().Not.Be.Empty();
87 list.Should().Contain.Item(item);
88 };
89
90 Here are some additional examples of assertions using the fluent API:
91
92 public void Should_fluent_assertions()
93 {
94 var numbers = new List<int> { 1, 1, 2, 3 };
95 numbers.Should().Contain.Any(x => x == 1);
96 numbers
97 .Should().Count.AtLeast(1)
98 .Should().Count.NoMoreThan(5)
99 .Should().Count.Exactly(4)
100 .Should().Contain.One(x => x > 2);
101
102 var id = new Guid();
103 id.Should().Be.Empty();
104
105 id = Guid.NewGuid();
106 id.Should().Not.Be.Empty();
107
108 var date = DateTime.Now;
109 date1.Should().Be.Today();
110
111 var str = "";
112 str.Should().Be.NullOrEmpty();
113
114 var one = "1";
115 one.Should().Be.ConvertableTo<int>();
116
117 var idString = Guid.NewGuid().ToString();
118 idString.Should().Be.ConvertableTo<Guid>();
119 }
Something went wrong with that request. Please try again.