Skip to content

Commit c965502

Browse files
committed
added specs
1 parent 370ab3c commit c965502

File tree

5 files changed

+148
-106
lines changed

5 files changed

+148
-106
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
nbproject/
1+
nbproject/
2+
test
Lines changed: 73 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,115 @@
11
require File.dirname(__FILE__) + '/spec_helper.rb'
22

3-
# Time to add your specs!
4-
# http://rspec.info/
5-
describe "setting up acts_as_api" do
3+
describe "acts_as_api" do
64

75
before(:each) do
86
@luke = Customer.new({ :firstname => 'Luke', :lastname => 'Skywalker', :age => 25, :active => true })
97
@han = Customer.new({ :firstname => 'Han', :lastname => 'Solo', :age => 35, :active => true })
108
@leia = Customer.new({ :firstname => 'Princess', :lastname => 'Leia', :age => 25, :active => false })
11-
12-
# always reset the api_accessible values
13-
Customer.write_inheritable_attribute(:api_accessible, Set.new )
149
end
1510

16-
it "should be disabled by default" do
17-
Customer.acts_as_api?.should be_false
18-
Customer.should_not respond_to :api_accessible
19-
# now enable it
20-
Customer.acts_as_api
21-
# should respond now
22-
Customer.acts_as_api?.should be_true
23-
Customer.should respond_to :api_accessible
11+
describe "acts_as_api is disabled by default" do
12+
it "should indicate that acts_as_api is disabled" do
13+
Customer.acts_as_api?.should be_false
14+
end
15+
16+
it "should not respond to api_accessible" do
17+
Customer.should_not respond_to :api_accessible
18+
end
2419
end
2520

26-
it "check simple attributes list" do
27-
28-
Customer.api_accessible :v1_default => [ :firstname, :lastname ]
21+
describe "acts_as_api is enabled" do
2922

30-
response = @luke.as_api_response(:v1_default)
23+
before(:each) do
24+
Customer.acts_as_api
25+
end
3126

32-
response.should be_kind_of(Hash)
27+
it "should indicate that acts_as_api is enabled" do
28+
Customer.acts_as_api?.should be_true
29+
end
3330

34-
response.should have(2).keys
31+
it "should not respond to api_accessible" do
32+
Customer.should respond_to :api_accessible
33+
end
3534

36-
response.keys.should include(:firstname, :lastname)
35+
describe "listing attributes in the api template" do
3736

38-
response.values.should include(@luke.firstname, @luke.lastname)
37+
before(:each) do
38+
Customer.api_accessible :v1_default => [ :firstname, :lastname ]
39+
@response = @luke.as_api_response(:v1_default)
40+
end
41+
42+
it "should return a hash" do
43+
@response.should be_kind_of(Hash)
44+
end
3945

40-
end
46+
it "should return the correct number of keys" do
47+
@response.should have(2).keys
48+
end
4149

42-
it "check method call in attributes list" do
50+
it "should return all specified fields" do
51+
@response.keys.should include(:firstname, :lastname)
52+
end
4353

44-
Customer.api_accessible :v1_only_full_name => [ :full_name ]
54+
it "should return the correct values for the specified fields" do
55+
@response.values.should include(@luke.firstname, @luke.lastname)
56+
end
57+
58+
end
4559

46-
response = @luke.as_api_response(:v1_only_full_name)
60+
describe "calling a method in the api template" do
4761

48-
response.should be_kind_of(Hash)
62+
before(:each) do
63+
Customer.api_accessible :v1_only_full_name => [ :full_name ]
64+
@response = @luke.as_api_response(:v1_only_full_name)
65+
end
4966

50-
response.should have(1).keys
67+
it "should return a hash" do
68+
@response.should be_kind_of(Hash)
69+
end
5170

52-
response.keys.should include(:full_name)
71+
it "should return the correct number of keys" do
72+
@response.should have(1).key
73+
end
5374

54-
response.values.should include(@luke.full_name)
75+
it "should return all specified fields" do
76+
@response.keys.should include(:full_name)
77+
end
5578

56-
end
79+
it "should return the correct values for the specified fields" do
80+
@response.values.should include(@luke.full_name)
81+
end
5782

83+
end
5884

59-
it "check renaming the node/key of an attribute" do
85+
describe "renaming an attribute in the api template" do
86+
87+
end
6088

61-
end
89+
describe "renaming the node/key of a method in the api template" do
6290

63-
it "check renaming the node/key of a method" do
91+
end
6492

65-
end
66-
67-
it "check included associations in attributes list (DON'T act_as_api themselves)" do
68-
69-
end
93+
describe "including a scoped association in the api template" do
7094

95+
end
7196

72-
it "check included associations in attributes list (DO act_as_api themselves)" do
97+
describe "including an association (which doesn't acts_as_api) in the api template" do
98+
99+
end
73100

74-
end
101+
describe "including an association (which does acts_as_api) in the api template" do
75102

76-
it "check creating a sub node and putting an attribute in it" do
103+
end
77104

78-
end
105+
describe "creating a sub node in the api template and putting an attribute in it" do
79106

107+
end
80108

109+
describe "creating multiple sub nodes in the api template and putting an attribute in it" do
81110

82-
it "check creating multiple sub nodes and putting an attribute in it" do
111+
end
83112

84113
end
85114

86-
87115
end

spec/acts_as_api_spec.rb

Lines changed: 73 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,115 @@
11
require File.dirname(__FILE__) + '/spec_helper.rb'
22

3-
# Time to add your specs!
4-
# http://rspec.info/
5-
describe "setting up acts_as_api" do
3+
describe "acts_as_api" do
64

75
before(:each) do
86
@luke = Customer.new({ :firstname => 'Luke', :lastname => 'Skywalker', :age => 25, :active => true })
97
@han = Customer.new({ :firstname => 'Han', :lastname => 'Solo', :age => 35, :active => true })
108
@leia = Customer.new({ :firstname => 'Princess', :lastname => 'Leia', :age => 25, :active => false })
11-
12-
# always reset the api_accessible values
13-
Customer.write_inheritable_attribute(:api_accessible, Set.new )
149
end
1510

16-
it "should be disabled by default" do
17-
Customer.acts_as_api?.should be_false
18-
Customer.should_not respond_to :api_accessible
19-
# now enable it
20-
Customer.acts_as_api
21-
# should respond now
22-
Customer.acts_as_api?.should be_true
23-
Customer.should respond_to :api_accessible
11+
describe "acts_as_api is disabled by default" do
12+
it "should indicate that acts_as_api is disabled" do
13+
Customer.acts_as_api?.should be_false
14+
end
15+
16+
it "should not respond to api_accessible" do
17+
Customer.should_not respond_to :api_accessible
18+
end
2419
end
2520

26-
it "check simple attributes list" do
27-
28-
Customer.api_accessible :v1_default => [ :firstname, :lastname ]
21+
describe "acts_as_api is enabled" do
2922

30-
response = @luke.as_api_response(:v1_default)
23+
before(:each) do
24+
Customer.acts_as_api
25+
end
3126

32-
response.should be_kind_of(Hash)
27+
it "should indicate that acts_as_api is enabled" do
28+
Customer.acts_as_api?.should be_true
29+
end
3330

34-
response.should have(2).keys
31+
it "should not respond to api_accessible" do
32+
Customer.should respond_to :api_accessible
33+
end
3534

36-
response.keys.should include(:firstname, :lastname)
35+
describe "listing attributes in the api template" do
3736

38-
response.values.should include(@luke.firstname, @luke.lastname)
37+
before(:each) do
38+
Customer.api_accessible :v1_default => [ :firstname, :lastname ]
39+
@response = @luke.as_api_response(:v1_default)
40+
end
41+
42+
it "should return a hash" do
43+
@response.should be_kind_of(Hash)
44+
end
3945

40-
end
46+
it "should return the correct number of keys" do
47+
@response.should have(2).keys
48+
end
4149

42-
it "check method call in attributes list" do
50+
it "should return all specified fields" do
51+
@response.keys.should include(:firstname, :lastname)
52+
end
4353

44-
Customer.api_accessible :v1_only_full_name => [ :full_name ]
54+
it "should return the correct values for the specified fields" do
55+
@response.values.should include(@luke.firstname, @luke.lastname)
56+
end
57+
58+
end
4559

46-
response = @luke.as_api_response(:v1_only_full_name)
60+
describe "calling a method in the api template" do
4761

48-
response.should be_kind_of(Hash)
62+
before(:each) do
63+
Customer.api_accessible :v1_only_full_name => [ :full_name ]
64+
@response = @luke.as_api_response(:v1_only_full_name)
65+
end
4966

50-
response.should have(1).keys
67+
it "should return a hash" do
68+
@response.should be_kind_of(Hash)
69+
end
5170

52-
response.keys.should include(:full_name)
71+
it "should return the correct number of keys" do
72+
@response.should have(1).key
73+
end
5374

54-
response.values.should include(@luke.full_name)
75+
it "should return all specified fields" do
76+
@response.keys.should include(:full_name)
77+
end
5578

56-
end
79+
it "should return the correct values for the specified fields" do
80+
@response.values.should include(@luke.full_name)
81+
end
5782

83+
end
5884

59-
it "check renaming the node/key of an attribute" do
85+
describe "renaming an attribute in the api template" do
86+
87+
end
6088

61-
end
89+
describe "renaming the node/key of a method in the api template" do
6290

63-
it "check renaming the node/key of a method" do
91+
end
6492

65-
end
66-
67-
it "check included associations in attributes list (DON'T act_as_api themselves)" do
68-
69-
end
93+
describe "including a scoped association in the api template" do
7094

95+
end
7196

72-
it "check included associations in attributes list (DO act_as_api themselves)" do
97+
describe "including an association (which doesn't acts_as_api) in the api template" do
98+
99+
end
73100

74-
end
101+
describe "including an association (which does acts_as_api) in the api template" do
75102

76-
it "check creating a sub node and putting an attribute in it" do
103+
end
77104

78-
end
105+
describe "creating a sub node in the api template and putting an attribute in it" do
79106

107+
end
80108

109+
describe "creating multiple sub nodes in the api template and putting an attribute in it" do
81110

82-
it "check creating multiple sub nodes and putting an attribute in it" do
111+
end
83112

84113
end
85114

86-
87115
end

spec/spec_models.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,6 @@ class Customer < ActiveRecord::Base
22

33
has_many :orders
44

5-
#acts_as_api
6-
7-
# define the accessible attributes/methods for the api response
8-
#api_accessible :firstname, :age, :created_at, :lastname, :full_name,
9-
# include associated model in response
10-
# :orders,
11-
# rename the key for orders
12-
# :renamed_orders => :orders,
13-
# put orders in another subnode
14-
# :subnode_orders => { :sub_oders => :orders },
15-
# rename nodes/tag names
16-
# :other_node => :say_something,
17-
# create a deeper node hierarchy
18-
# :maybe => { :useful => { :for => :say_something } }
19-
205
def full_name
216
'' << firstname.to_s << ' ' << lastname.to_s
227
end

test

-8 KB
Binary file not shown.

0 commit comments

Comments
 (0)