Test rails controller in different formats

I have the following function in the controller

def by_xy @obj = BldPoly::find_by_xy(:x => params['x'], :y => params['y']) respond_to do |format| format.html { render :layout => false } format.xml { render :layout => false } format.json { render :layout => false } end 

and plan to record an automatic test as follows

 xml = nil get :by_xy, {:x => 4831, :y => 3242, :format => :json} assert_nothing_thrown { xml = REXML::Document.new(@response.body) } td = REXML::XPath.first(xml, "//result/item") assert_equal need_value, td.value 

and I get Completed in 50 ms (View: 0, DB: 230) | 406 Not acceptable [ http://test.host/search/by_xy/4831/3242.json]

when I missed the format in the test code - everything works correctly,

How can I write a test?

+6
ruby-on-rails automated-tests
source share
2 answers

I really understood this; That is how it should be

 get :by_xy, {:x => i[:x], :y => i[:y]}, :format => :json 
+8
source share

For 5.1 rails, while writing, I had to include the format attribute inside the params hash

 share_params = { email: nil, message: 'Default message.' format: :json } post image_share_path(@image), params: share_params assert_response :unprocessable_entity 

If I didn’t get an ActionController::UnknownFormat inside my creation controller

 def create @image = Image.new(image_params) if @image.save flash[:success] = 'Your image was saved successfully.' redirect_to @image else respond_to do |format| format.json do render json: { @image.to_json }, status: :unprocessable_entity end end end 
0
source share

All Articles