Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Shoulda assertions as MT::Expectations #8

Merged
merged 1 commit into from Apr 10, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 55 additions & 0 deletions README.md
Expand Up @@ -80,6 +80,61 @@ describe PostsController, "#show" do
end 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 ## Contributing


Expand Down
1 change: 1 addition & 0 deletions lib/minitest/rails/shoulda.rb
@@ -1,4 +1,5 @@
require "minitest/rails/shoulda/version" require "minitest/rails/shoulda/version"
require "minitest/rails/shoulda/assertions" require "minitest/rails/shoulda/assertions"
require "minitest/rails/shoulda/expectations"
require "minitest/rails/shoulda/dsl" require "minitest/rails/shoulda/dsl"
require "minitest/rails/shoulda/matchers" require "minitest/rails/shoulda/matchers"
42 changes: 42 additions & 0 deletions 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
48 changes: 48 additions & 0 deletions 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