Skip to content

Commit 5a0fbed

Browse files
committed
fixe specs
1 parent abde072 commit 5a0fbed

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

lib/gon.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ def all_variables
1717
@request_env[:gon]
1818
end
1919

20+
def all_variables=(values)
21+
raise "You can't use Gon public methods for storing data"
22+
end
23+
2024
def clear
2125
@request_env[:gon] = {}
2226
end
@@ -37,7 +41,7 @@ def request
3741
end
3842

3943
def request=(request_id)
40-
@request_id = request_id
44+
@request_id = request_id
4145
end
4246

4347
def method_missing(m, *args, &block)
@@ -61,10 +65,10 @@ def set_variable(name, value)
6165

6266
def rabl(view_path, options = {})
6367
if !defined?(Gon::Rabl)
64-
raise NoMethodError.new('You should define Rabl in your Gemfile')
68+
raise NoMethodError.new('You should define Rabl in your Gemfile')
6569
end
66-
rabl_data = Gon::Rabl.parse_rabl(view_path, options[:controller] ||
67-
@request_env['action_controller.instance'] ||
70+
rabl_data = Gon::Rabl.parse_rabl(view_path, options[:controller] ||
71+
@request_env['action_controller.instance'] ||
6872
@request_env['action_controller.rescue.response'].
6973
instance_variable_get('@template').
7074
instance_variable_get('@controller'))
@@ -81,13 +85,13 @@ def rabl(view_path, options = {})
8185

8286
def jbuilder(view_path, options = {})
8387
if RUBY_VERSION !~ /9/
84-
raise NoMethodError.new('You can use Jbuilder support only in 1.9+')
88+
raise NoMethodError.new('You can use Jbuilder support only in 1.9+')
8589
elsif !defined?(Gon::Jbuilder)
86-
raise NoMethodError.new('You should define Jbuilder in your Gemfile')
90+
raise NoMethodError.new('You should define Jbuilder in your Gemfile')
8791
end
8892

89-
jbuilder_data = Gon::Jbuilder.parse_jbuilder(view_path, options[:controller] ||
90-
@request_env['action_controller.instance'] ||
93+
jbuilder_data = Gon::Jbuilder.parse_jbuilder(view_path, options[:controller] ||
94+
@request_env['action_controller.instance'] ||
9195
@request_env['action_controller.rescue.response'].
9296
instance_variable_get('@template').
9397
instance_variable_get('@controller'))

spec/gon/gon_spec.rb

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# gon_spec_rb
22
require 'gon'
33

4-
describe Gon, '#all_variables' do
4+
describe Gon, '#all_variables' do
55

66
before(:each) do
77
Gon.request_env = {}
@@ -41,13 +41,26 @@
4141
lambda { Gon.all_variables = 123 }.should raise_error
4242
end
4343

44-
it 'render json from rabl template' do
45-
Gon.clear
46-
controller = ActionController::Base.new
47-
objects = [1,2]
48-
controller.instance_variable_set('@objects', objects)
49-
Gon.rabl 'spec/test_data/sample.rabl', :controller => controller
50-
Gon.objects.length.should == 2
44+
context 'render json from rabl template' do
45+
before :each do
46+
Gon.clear
47+
controller.instance_variable_set('@objects', objects)
48+
end
49+
50+
let(:controller) { ActionController::Base.new}
51+
let(:objects) { [1,2]}
52+
53+
it 'raises an error if rabl is not included' do
54+
expect { Gon.rabl 'spec/test_data/sample.rabl', :controller => controller}.to raise_error
55+
end
56+
57+
it 'works if rabl is included' do
58+
require 'rabl'
59+
require 'gon/rabl'
60+
Gon.rabl 'spec/test_data/sample.rabl', :controller => controller
61+
Gon.objects.length.should == 2
62+
end
63+
5164
end
5265

5366
if RUBY_VERSION =~ /1.9/

0 commit comments

Comments
 (0)