Permalink
Browse files

Expose Shoulda assertions as MT::Expectations

Adds:

* must_contain
* wont_contain
* must_have_same_elements
* wont_have_same_elements

Updates:

* README.md
  • Loading branch information...
1 parent 9015b00 commit 3c26289703b3c52b4ff373d33afe987c5d9b6bee @elskwid elskwid committed Apr 9, 2013
Showing with 146 additions and 0 deletions.
  1. +55 −0 README.md
  2. +1 −0 lib/minitest/rails/shoulda.rb
  3. +42 −0 lib/minitest/rails/shoulda/expectations.rb
  4. +48 −0 test/test_expectations.rb
View
55 README.md
@@ -80,6 +80,61 @@ describe PostsController, "#show" do
end
```
+### Shoulda Assertions (Expectations)
+
+Assertions:
+
+```ruby
+# collection contains some value
+assert_contains collection, "a"
+assert_contains collection, /a/
+assert_contains collection, 3
+
+# or
+
+refute_does_not_contain collection, "a"
+refute_does_not_contain collection, /a/
+refute_does_not_contain collection, 3
+
+
+# collection does not contain some value
+refute_contains collection, "b"
+refute_contains collection, /b/
+refute_contains collection, 5
+
+# or
+
+assert_does_not_contain collection, "b"
+assert_does_not_contain collection, /b/
+assert_does_not_contain collection, 5
+
+# collections contain the same elements
+assert_same_elements [1, b, "3"], ["3", 1, b]
+
+# collection does not contain the same elements
+refute_same_elements [1, b, "3"], ["4", b, 1]
+```
+
+And as Expectations:
+
+```ruby
+# collection contains some value
+collection.must_contain "a"
+collection.must_contain /a/
+collection.must_contain 3
+
+# collection does not contain some value
+collection.wont_contain "b"
+collection.wont_contain /b/
+collection.wont_contain 5
+
+# collections contain the same elements
+[1, b, "3"].must_have_same_elements ["3", 1, b]
+
+# collection does not contain the same elements
+[1, b, "3"].wont_have_same_elements ["4", b, 1]
+```
+
## Contributing
View
1 lib/minitest/rails/shoulda.rb
@@ -1,4 +1,5 @@
require "minitest/rails/shoulda/version"
require "minitest/rails/shoulda/assertions"
+require "minitest/rails/shoulda/expectations"
require "minitest/rails/shoulda/dsl"
require "minitest/rails/shoulda/matchers"
View
42 lib/minitest/rails/shoulda/expectations.rb
@@ -0,0 +1,42 @@
+module MiniTest
+ module Expectations
+
+ ##
+ # See MiniTest::Assertions#assert_contains
+ #
+ # collection.must_contain "a"
+ # collection.must_contain /a/
+ #
+ # :method: must_contain
+
+ infect_an_assertion :assert_contains, :must_contain, :reverse
+
+ ##
+ # See MiniTest::Assertions#assert_does_not_contain
+ #
+ # collection.wont_contain "b"
+ # collection.wont_contain /b/
+ #
+ # :method: wont_contain
+
+ infect_an_assertion :assert_does_not_contain, :wont_contain, :reverse
+
+ ##
+ # See MiniTest::Assertions#assert_same_elements
+ #
+ # collection.must_have_same_elements ["a", b, 1]
+ #
+ # :method: must_have_same_elements
+
+ infect_an_assertion :assert_same_elements, :must_have_same_elements
+
+ ##
+ # See MiniTest::Assertions#refute_same_elements
+ #
+ # collection.wont_have_same_elements ["b", c, 3]
+ #
+ # :method: wont_have_same_elements
+
+ infect_an_assertion :refute_same_elements, :wont_have_same_elements
+ end
+end
View
48 test/test_expectations.rb
@@ -0,0 +1,48 @@
+require "minitest/autorun"
+require "minitest/rails/shoulda/dsl"
+require "minitest/rails/shoulda/assertions"
+require "minitest/rails/shoulda/expectations"
+
+describe "Shoulda Style Expectations" do
+
+ context "an array of values" do
+
+ setup do
+ @a = ['abc', 'def', 3]
+ end
+
+ [/b/, 'abc', 3].each do |x|
+ should "contain #{x.inspect}" do
+ proc { @a.wont_contain x }.must_raise(MiniTest::Assertion)
+
+ @a.must_contain x
+ end
+ end
+
+ should "not contain 'wtf'" do
+ proc { @a.must_contain 'wtf' }.must_raise(MiniTest::Assertion)
+
+ @a.wont_contain 'wtf'
+ end
+
+ should "be the same as another array, ordered differently" do
+ @a.must_have_same_elements [3, "def", "abc"]
+
+ proc {
+ @a.must_have_same_elements [3, 3, "def", "abc"]
+ }.must_raise(MiniTest::Assertion)
+
+ @a.wont_have_same_elements [3, 3, "def", "abc"]
+
+ [@a, "abc"].flatten.must_have_same_elements ["abc", 3, "def", "abc"]
+
+ proc {
+ [@a, "abc"].flatten.must_have_same_elements [3, 3, "def", "abc"]
+ }.must_raise(MiniTest::Assertion)
+
+ [@a, "abc"].flatten.wont_have_same_elements [3, 3, "def", "abc"]
+ end
+
+ end
+
+end

0 comments on commit 3c26289

Please sign in to comment.