This repository has been archived by the owner on Feb 25, 2022. It is now read-only.
/
associations_spec.rb
130 lines (104 loc) · 3.63 KB
/
associations_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
require "spec_helper"
describe Remotely::Associations do
let(:app) { "http://localhost:1234" }
shared_examples_for "an association" do
it "keeps track of it's remote associations" do
subject.remote_associations.should include(assoc)
end
it "creates a method for the association" do
subject.should respond_to(assoc)
end
end
shared_examples_for "an association with a path" do
it "generates the correct path" do
subject.path_to(assoc, type).should == path
end
it "requests the correct path" do
subject.send(assoc)
a_request(:get, "#{app}#{path}").should have_been_made
end
end
describe "has_many_remote" do
subject { HasMany.new(id: 1) }
let(:type) { :has_many }
let(:assoc) { :things }
it_behaves_like "an association"
it "returns a Collection" do
subject.things.should be_a Remotely::Collection
end
it "returns a Collection of the appropriate model" do
subject.things.first.should be_a Thing
end
context "with no options" do
subject { HasMany.new(id: 1) }
let(:path) { "/has_manies/1/things" }
it_behaves_like "an association with a path"
end
context "with the :path option" do
subject { HasManyWithPath.new(id: 1) }
let(:path) { "/custom/things" }
it_behaves_like "an association with a path"
end
context "with :path variables" do
subject { HasManyWithPathVariables.new(name: "stuff") }
let(:path) { "/custom/stuff/things" }
it_behaves_like "an association with a path"
end
context "with the :foreign_key option" do
subject { HasManyWithForeignKey.new }
specify { expect { subject.path_to(:things, :has_many) }.to raise_error(Remotely::HasManyForeignKeyError) }
end
end
describe "has_one_remote" do
subject { HasOne.new(id: 1) }
let(:type) { :has_one }
let(:assoc) { :thing }
it_behaves_like "an association"
it "returns an object of the appropriate model" do
subject.thing.should be_a Thing
end
context "with no options" do
subject { HasOne.new(id: 1) }
let(:path) { "/has_ones/1/thing" }
it_behaves_like "an association with a path"
end
context "with the :path option" do
subject { HasOneWithPath.new(id: 1) }
let(:path) { "/custom/thing" }
it_behaves_like "an association with a path"
end
context "with :path variables" do
subject { HasOneWithPathVariables.new(name: "stuff") }
let(:path) { "/custom/stuff/thing" }
it_behaves_like "an association with a path"
end
context "with the :foreign_key option" do
subject { HasOneWithForeignKey.new }
specify { expect { subject.path_to(:thing, :has_one) }.to raise_error(Remotely::HasManyForeignKeyError) }
end
end
describe "belongs_to_remote" do
subject { BelongsTo.new(id: 1, thing_id: 1) }
let(:type) { :belongs_to }
let(:assoc) { :thing }
it_behaves_like "an association"
it "returns an object of the appropriate model" do
subject.thing.should be_a Thing
end
context "with no options" do
subject { BelongsTo.new(thing_id: 1) }
let(:path) { "/things/1" }
it_behaves_like "an association with a path"
end
context "with the :path option" do
subject { BelongsToWithPath.new }
let(:path) { "/custom/thing" }
it_behaves_like "an association with a path"
end
context "with :path variables" do
subject { BelongsToWithPathVariables.new(name: "stuff") }
let(:path) { "/custom/stuff/thing" }
it_behaves_like "an association with a path"
end
end
end