-
Notifications
You must be signed in to change notification settings - Fork 111
Add support for MiniTest #36
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
require "bundler/gem_tasks" | ||
require "rspec/core/rake_task" | ||
require "cucumber/rake/task" | ||
require "rake/testtask" | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
Cucumber::Rake::Task.new(:cucumber) | ||
|
||
task :test => [:spec, :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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,9 +25,19 @@ 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 | ||
|
||
RSpec.configure do |config| | ||
config.include JsonSpec::Matchers | ||
if defined?(RSpec) | ||
RSpec.configure do |config| | ||
config.include JsonSpec::Matchers | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we are expanding these matchers to work elsewhere I'd rather have all configuration taken care either in its own file or required by the user in the test helper. For rspec we could require the user to simply add this configuration to their spec_helper. |
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
require "rspec" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rspec is not required for the spec_helper |
||
require "json_spec" | ||
|
||
RSpec.configure do |config| | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require "test_helper" | ||
|
||
class UserToJsonTest < MiniTest::Unit::TestCase | ||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require "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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require "minitest/autorun" | ||
require "minitest/matchers" | ||
require "json_spec" | ||
|
||
# Make JsonSpec avaialble in tests | ||
class MiniTest::Unit::TestCase | ||
include JsonSpec::Matchers | ||
end | ||
|
||
# Give OpenStruct support for to_json | ||
require "ostruct" | ||
|
||
class OpenStruct | ||
def to_json *args | ||
table.to_json *args | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this can be removed as well