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

add support for MiniTest #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 77 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,85 @@ You can also remember JSON inline:
Then the JSON response at "0/first_name" should be %{FIRST_NAME}
```

MiniTest
--------

json_spec matchers can be used in MiniTest tests. To use json_spec in your tests
you must use the minitest-matchers gem. In your `test_helper.rb` you must:

```ruby
require "minitest/autorun"
require "minitest/matchers"
require "json_spec"
```

The matchers are now available in MiniTest's spec DSL as follows:

```ruby
describe User, :to_json do
let(:user) { User.create! first_name: "Steve", last_name: "Richert" }
let(:names) { %({"first_name":"Steve","last_name":"Richert"}) }

it "includes names" do
user.to_json.must be_json_eql(names).excluding("friends")
end

it "includes the ID" do
user.to_json.must have_json_path("id")
user.to_json.must have_json_type(Integer).at_path("id")
end

it "includes friends" do
user.to_json.must have_json_size(0).at_path("friends")

friend = User.create! first_name: "Catie", last_name: "Richert"
user.friends << friend

user.to_json.must have_json_size(1).at_path("friends")
user.to_json.must include_json(friend.to_json).at_path("friends")
end
end
```

The matchers are also available through MiniTest's classic assertions as follows:

```ruby
class UserToJsonTest < MiniTest::Test
def setup
@user = User.create! first_name: "Steve", last_name: "Richert"
@names = %({"first_name":"Steve","last_name":"Richert"})
end

def test_includes_names
assert_must be_json_eql(@names).excluding("friends"), @user.to_json
end

def test_includes_id
assert_have_json_path @user.to_json, "id"
assert_must have_json_path("id"), @user.to_json

assert_must have_json_type(Integer).at_path("id"), @user.to_json
end

def test_includes_friends
assert_must have_json_size(0).at_path("friends"), @user.to_json

friend = User.create! first_name: "Catie", last_name: "Richert"
@user.friends << friend

assert_must have_json_size(1).at_path("friends"), @user.to_json
assert_must include_json(friend.to_json).at_path("friends"), @user.to_json
end
end
```

### More

Check out the [specs](https://github.com/collectiveidea/json_spec/blob/master/spec)
and [features](https://github.com/collectiveidea/json_spec/blob/master/features) to see all the
various ways you can use json_spec.
Check out the different test suites to see all the various ways you can use json_spec:

* [RSpec specs](https://github.com/collectiveidea/json_spec/blob/master/spec)
* [MiniTest tests](https://github.com/collectiveidea/json_spec/blob/master/test)
* [Cucumber features](https://github.com/collectiveidea/json_spec/blob/master/features)

## Contributing

Expand Down
11 changes: 9 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"
require "cucumber/rake/task"
require "rake/testtask"

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

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

task test: [:spec, :cucumber, :negative_cucumber]
task default: :test
Rake::TestTask.new do |t|
t.libs << "test" << "lib"
t.test_files = FileList['test/**/*_test.rb']
t.verbose = true
end

task :all => [:spec, :cucumber, :test]
task :default => :all
2 changes: 1 addition & 1 deletion features/support/env.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$: << File.expand_path("../../../lib", __FILE__)
$LOAD_PATH << ::File.expand_path('../../../lib', __FILE__)

require "json_spec/cucumber"

Expand Down
9 changes: 6 additions & 3 deletions json_spec.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ Gem::Specification.new do |gem|

gem.authors = ["Steve Richert"]
gem.email = ["steve.richert@gmail.com"]
gem.summary = "Easily handle JSON in RSpec and Cucumber"
gem.description = "RSpec matchers and Cucumber steps for testing JSON content"
gem.summary = "Easily handle JSON in RSpec, MiniTest and Cucumber"
gem.description = "RSpec/MiniTest matchers and Cucumber steps for testing JSON content"
gem.homepage = "https://github.com/collectiveidea/json_spec"
gem.license = "MIT"

gem.add_dependency "multi_json", "~> 1.0"
gem.add_dependency "rspec", ">= 2.0", "< 4.0"

gem.add_development_dependency "rspec", ">= 2.0", "< 4.0"
gem.add_development_dependency "cucumber"
gem.add_development_dependency "minitest", "~> 5.0"
gem.add_development_dependency "minitest-matchers", "~> 1.4"
gem.add_development_dependency "bundler", "~> 1.0"
gem.add_development_dependency "rake", "~> 10.0"

Expand Down
1 change: 0 additions & 1 deletion lib/json_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "json"
require "rspec"
require "json_spec/errors"
require "json_spec/configuration"
require "json_spec/exclusion"
Expand Down
20 changes: 18 additions & 2 deletions lib/json_spec/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,25 @@ def have_json_type(type)
def have_json_size(size)
JsonSpec::Matchers::HaveJsonSize.new(size)
end

def self.included(base)
if base.respond_to?(:register_matcher)
instance_methods.each do |name|
base.register_matcher name, name
end
end
end
end
end

if defined?(RSpec)
RSpec.configure do |config|
config.include JsonSpec::Matchers
end
end

RSpec.configure do |config|
config.include JsonSpec::Matchers
if defined?(MiniTest)
class Minitest::Test
include JsonSpec::Matchers
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
$LOAD_PATH << ::File.expand_path('../../lib', __FILE__)

require "json_spec"

RSpec.configure do |config|
Expand Down
29 changes: 29 additions & 0 deletions test/assertions_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require_relative "test_helper"

class UserToJsonTest < Minitest::Test
def setup
@user = OpenStruct.new id: 42, first_name: "Steve", last_name: "Richert", friends: []
@names = %({"first_name":"Steve","last_name":"Richert"})
end

def test_includes_names
assert_must be_json_eql(@names).excluding("friends"), @user.to_json
end

def test_includes_id
assert_have_json_path @user.to_json, "id"
assert_must have_json_path("id"), @user.to_json

assert_must have_json_type(Integer).at_path("id"), @user.to_json
end

def test_includes_friends
assert_must have_json_size(0).at_path("friends"), @user.to_json

friend = OpenStruct.new first_name: "Catie", last_name: "Richert"
@user.friends << friend

assert_must have_json_size(1).at_path("friends"), @user.to_json
assert_must include_json(friend.to_json).at_path("friends"), @user.to_json
end
end
38 changes: 38 additions & 0 deletions test/expectations_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require_relative "test_helper"

describe "User", :to_json do
let(:user) { OpenStruct.new id: 42, first_name: "Steve", last_name: "Richert", friends: [] }
let(:names) { %({"first_name":"Steve","last_name":"Richert"}) }
subject { user.to_json }

it "includes names" do
user.to_json.must be_json_eql(names).excluding("friends")
end

it { must be_json_eql(names).excluding("friends") }
must { be_json_eql(names).excluding("friends") }

it "includes the ID" do
user.to_json.must have_json_path("id")
user.to_json.must have_json_type(Integer).at_path("id")
end

it { must have_json_path("id") }
must { have_json_path("id") }

it { must have_json_type(Integer).at_path("id") }
must { have_json_type(Integer).at_path("id") }

it "includes friends" do
user.to_json.must have_json_size(0).at_path("friends")

friend = OpenStruct.new first_name: "Catie", last_name: "Richert"
user.friends << friend

user.to_json.must have_json_size(1).at_path("friends")
user.to_json.must include_json(friend.to_json).at_path("friends")
end

it { must have_json_size(0).at_path("friends") }
must { have_json_size(0).at_path("friends") }
end
14 changes: 14 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
$LOAD_PATH << ::File.expand_path('../../lib', __FILE__)

require "minitest/autorun"
require "minitest/matchers"
require "json_spec"

# Give OpenStruct support for to_json
require "ostruct"

class OpenStruct
def to_json *args
table.to_json *args
end
end