spec/gon/jbuilder_spec.rb in gon-5.2.3 vs spec/gon/jbuilder_spec.rb in gon-6.0.0
- old
+ new
@@ -12,24 +12,24 @@
let(:controller) { ActionController::Base.new }
let(:objects) { [1, 2] }
it 'render json from jbuilder template' do
- Gon.jbuilder 'spec/test_data/sample.json.jbuilder', :controller => controller
+ Gon.jbuilder :template => 'spec/test_data/sample.json.jbuilder', :controller => controller
expect(Gon.objects.length).to eq(2)
end
it 'render json from jbuilder template with locals' do
- Gon.jbuilder 'spec/test_data/sample_with_locals.json.jbuilder',
+ Gon.jbuilder :template => 'spec/test_data/sample_with_locals.json.jbuilder',
:controller => controller,
:locals => { :some_local => 1234, :some_complex_local => OpenStruct.new(:id => 1234) }
expect(Gon.some_local).to eq(1234)
expect(Gon.some_complex_local_id).to eq(1234)
end
it 'render json from jbuilder template with locals' do
- Gon.jbuilder 'spec/test_data/sample_with_helpers.json.jbuilder', :controller => controller
+ Gon.jbuilder :template => 'spec/test_data/sample_with_helpers.json.jbuilder', :controller => controller
expect(Gon.date).to eq('about 6 hours')
end
it 'render json from jbuilder template with controller methods' do
class << controller
@@ -38,33 +38,40 @@
end
helper_method :private_controller_method
private :private_controller_method
end
- Gon.jbuilder 'spec/test_data/sample_with_controller_method.json.jbuilder', :controller => controller
+ Gon.jbuilder :template => 'spec/test_data/sample_with_controller_method.json.jbuilder', :controller => controller
expect(Gon.data_from_method).to eq('gon test helper works')
end
it 'render json from jbuilder template with a partial' do
controller.view_paths << 'spec/test_data'
- Gon.jbuilder 'spec/test_data/sample_with_partial.json.jbuilder', :controller => controller
+ Gon.jbuilder :template => 'spec/test_data/sample_with_partial.json.jbuilder', :controller => controller
expect(Gon.objects.length).to eq(2)
end
- end
+ context 'within Rails' do
+ before do
+ module ::Rails
+ end
- it 'should raise error if you use gon.jbuilder without requiring jbuilder gem' do
- Gon.send(:remove_const, :Jbuilder)
+ allow(Rails).to receive_message_chain("application.routes.url_helpers.instance_methods") { [:user_path] }
+ controller.instance_variable_set('@user_id', 1)
+ end
- expect { Gon.jbuilder 'some_path' }.to raise_error
- load 'jbuilder.rb'
- load 'gon/jbuilder.rb'
- end
+ after do
+ Object.send(:remove_const, :Rails)
+ end
- end
+ it 'includes url_helpers' do
+ expect(controller).to receive(:user_path) { |id| "/users/#{id}" }
+ Gon.jbuilder :template => 'spec/test_data/sample_url_helpers.json.jbuilder', :controller => controller
+ expect(Gon.url).to eq '/users/1'
+ end
+ end
+ end
- def request
- @request ||= double 'request', :env => {}
end
end