-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
194 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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| | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |