Skip to content

Commit 4a479fd

Browse files
committed
add support for MiniTest
1 parent 00855fd commit 4a479fd

File tree

12 files changed

+237
-12
lines changed

12 files changed

+237
-12
lines changed

README.md

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,85 @@ You can also remember JSON inline:
312312
Then the JSON response at "0/first_name" should be %{FIRST_NAME}
313313
```
314314

315+
MiniTest
316+
--------
317+
318+
json_spec matchers can be used in MiniTest tests. To use json_spec in your tests
319+
you must use the minitest-matchers gem. In your `test_helper.rb` you must:
320+
321+
```ruby
322+
require "minitest/autorun"
323+
require "minitest/matchers"
324+
require "json_spec"
325+
```
326+
327+
The matchers are now available in MiniTest's spec DSL as follows:
328+
329+
```ruby
330+
describe User, :to_json do
331+
let(:user) { User.create! first_name: "Steve", last_name: "Richert" }
332+
let(:names) { %({"first_name":"Steve","last_name":"Richert"}) }
333+
334+
it "includes names" do
335+
user.to_json.must be_json_eql(names).excluding("friends")
336+
end
337+
338+
it "includes the ID" do
339+
user.to_json.must have_json_path("id")
340+
user.to_json.must have_json_type(Integer).at_path("id")
341+
end
342+
343+
it "includes friends" do
344+
user.to_json.must have_json_size(0).at_path("friends")
345+
346+
friend = User.create! first_name: "Catie", last_name: "Richert"
347+
user.friends << friend
348+
349+
user.to_json.must have_json_size(1).at_path("friends")
350+
user.to_json.must include_json(friend.to_json).at_path("friends")
351+
end
352+
end
353+
```
354+
355+
The matchers are also available through MiniTest's classic assertions as follows:
356+
357+
```ruby
358+
class UserToJsonTest < MiniTest::Test
359+
def setup
360+
@user = User.create! first_name: "Steve", last_name: "Richert"
361+
@names = %({"first_name":"Steve","last_name":"Richert"})
362+
end
363+
364+
def test_includes_names
365+
assert_must be_json_eql(@names).excluding("friends"), @user.to_json
366+
end
367+
368+
def test_includes_id
369+
assert_have_json_path @user.to_json, "id"
370+
assert_must have_json_path("id"), @user.to_json
371+
372+
assert_must have_json_type(Integer).at_path("id"), @user.to_json
373+
end
374+
375+
def test_includes_friends
376+
assert_must have_json_size(0).at_path("friends"), @user.to_json
377+
378+
friend = User.create! first_name: "Catie", last_name: "Richert"
379+
@user.friends << friend
380+
381+
assert_must have_json_size(1).at_path("friends"), @user.to_json
382+
assert_must include_json(friend.to_json).at_path("friends"), @user.to_json
383+
end
384+
end
385+
```
386+
315387
### More
316388

317-
Check out the [specs](https://github.com/collectiveidea/json_spec/blob/master/spec)
318-
and [features](https://github.com/collectiveidea/json_spec/blob/master/features) to see all the
319-
various ways you can use json_spec.
389+
Check out the different test suites to see all the various ways you can use json_spec:
390+
391+
* [RSpec specs](https://github.com/collectiveidea/json_spec/blob/master/spec)
392+
* [MiniTest tests](https://github.com/collectiveidea/json_spec/blob/master/test)
393+
* [Cucumber features](https://github.com/collectiveidea/json_spec/blob/master/features)
320394

321395
## Contributing
322396

Rakefile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require "bundler/gem_tasks"
22
require "rspec/core/rake_task"
33
require "cucumber/rake/task"
4+
require "rake/testtask"
45

56
RSpec::Core::RakeTask.new(:spec)
67

@@ -12,5 +13,11 @@ Cucumber::Rake::Task.new(:negative_cucumber) do |task|
1213
task.cucumber_opts = "--tags @fail --wip"
1314
end
1415

15-
task test: [:spec, :cucumber, :negative_cucumber]
16-
task default: :test
16+
Rake::TestTask.new do |t|
17+
t.libs << "test" << "lib"
18+
t.test_files = FileList['test/**/*_test.rb']
19+
t.verbose = true
20+
end
21+
22+
task :all => [:spec, :cucumber, :test]
23+
task :default => :all

features/support/env.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
$: << File.expand_path("../../../lib", __FILE__)
1+
$LOAD_PATH << ::File.expand_path('../../../lib', __FILE__)
22

33
require "json_spec/cucumber"
44

json_spec.gemspec

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ Gem::Specification.new do |gem|
66

77
gem.authors = ["Steve Richert"]
88
gem.email = ["[email protected]"]
9-
gem.summary = "Easily handle JSON in RSpec and Cucumber"
10-
gem.description = "RSpec matchers and Cucumber steps for testing JSON content"
9+
gem.summary = "Easily handle JSON in RSpec, MiniTest and Cucumber"
10+
gem.description = "RSpec/MiniTest matchers and Cucumber steps for testing JSON content"
1111
gem.homepage = "https://github.com/collectiveidea/json_spec"
1212
gem.license = "MIT"
1313

1414
gem.add_dependency "multi_json", "~> 1.0"
15-
gem.add_dependency "rspec", ">= 2.0", "< 4.0"
1615

16+
gem.add_development_dependency "rspec", ">= 2.0", "< 4.0"
17+
gem.add_development_dependency "cucumber"
18+
gem.add_development_dependency "minitest", "~> 4.0"
19+
gem.add_development_dependency "minitest-matchers", "~> 1.2"
1720
gem.add_development_dependency "bundler", "~> 1.0"
1821
gem.add_development_dependency "rake", "~> 10.0"
1922

lib/json_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require "json"
2-
require "rspec"
32
require "json_spec/errors"
43
require "json_spec/configuration"
54
require "json_spec/exclusion"

lib/json_spec/matchers.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,25 @@ def have_json_type(type)
2525
def have_json_size(size)
2626
JsonSpec::Matchers::HaveJsonSize.new(size)
2727
end
28+
29+
def self.included(base)
30+
if base.respond_to?(:register_matcher)
31+
instance_methods.each do |name|
32+
base.register_matcher name, name
33+
end
34+
end
35+
end
36+
end
37+
end
38+
39+
if defined?(RSpec)
40+
RSpec.configure do |config|
41+
config.include JsonSpec::Matchers
2842
end
2943
end
3044

31-
RSpec.configure do |config|
32-
config.include JsonSpec::Matchers
45+
if defined?(MiniTest)
46+
class Minitest::Test
47+
include JsonSpec::Matchers
48+
end
3349
end

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
$LOAD_PATH << ::File.expand_path('../../lib', __FILE__)
2+
13
require "json_spec"
24

35
RSpec.configure do |config|

test/assertions_test.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require_relative "test_helper"
2+
3+
class UserToJsonTest < Minitest::Test
4+
def setup
5+
@user = OpenStruct.new id: 42, first_name: "Steve", last_name: "Richert", friends: []
6+
@names = %({"first_name":"Steve","last_name":"Richert"})
7+
end
8+
9+
def test_includes_names
10+
assert_must be_json_eql(@names).excluding("friends"), @user.to_json
11+
end
12+
13+
def test_includes_id
14+
assert_have_json_path @user.to_json, "id"
15+
assert_must have_json_path("id"), @user.to_json
16+
17+
assert_must have_json_type(Integer).at_path("id"), @user.to_json
18+
end
19+
20+
def test_includes_friends
21+
assert_must have_json_size(0).at_path("friends"), @user.to_json
22+
23+
friend = OpenStruct.new first_name: "Catie", last_name: "Richert"
24+
@user.friends << friend
25+
26+
assert_must have_json_size(1).at_path("friends"), @user.to_json
27+
assert_must include_json(friend.to_json).at_path("friends"), @user.to_json
28+
end
29+
end

test/expectations_test.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require_relative "test_helper"
2+
3+
describe "User", :to_json do
4+
let(:user) { OpenStruct.new id: 42, first_name: "Steve", last_name: "Richert", friends: [] }
5+
let(:names) { %({"first_name":"Steve","last_name":"Richert"}) }
6+
subject { user.to_json }
7+
8+
it "includes names" do
9+
user.to_json.must be_json_eql(names).excluding("friends")
10+
end
11+
12+
it { must be_json_eql(names).excluding("friends") }
13+
must { be_json_eql(names).excluding("friends") }
14+
15+
it "includes the ID" do
16+
user.to_json.must have_json_path("id")
17+
user.to_json.must have_json_type(Integer).at_path("id")
18+
end
19+
20+
it { must have_json_path("id") }
21+
must { have_json_path("id") }
22+
23+
it { must have_json_type(Integer).at_path("id") }
24+
must { have_json_type(Integer).at_path("id") }
25+
26+
it "includes friends" do
27+
user.to_json.must have_json_size(0).at_path("friends")
28+
29+
friend = OpenStruct.new first_name: "Catie", last_name: "Richert"
30+
user.friends << friend
31+
32+
user.to_json.must have_json_size(1).at_path("friends")
33+
user.to_json.must include_json(friend.to_json).at_path("friends")
34+
end
35+
36+
it { must have_json_size(0).at_path("friends") }
37+
must { have_json_size(0).at_path("friends") }
38+
end

test/test_helper.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
$LOAD_PATH << ::File.expand_path('../../lib', __FILE__)
2+
3+
require "minitest/autorun"
4+
require "minitest/matchers"
5+
require "json_spec"
6+
7+
# Give OpenStruct support for to_json
8+
require "ostruct"
9+
10+
class OpenStruct
11+
def to_json *args
12+
table.to_json *args
13+
end
14+
end

0 commit comments

Comments
 (0)