javascript - How to test json formated actions in Rails 4 -


i'm trying test rails app actions return json formated data. example, inside userscontroller

  # post /users.json   def create     @user = user.new(user_params)      respond_to |format|       if @user.save         format.json { render json: @user, status: :created }       else         format.json { render json: @user.errors, status: :unprocessable_entity }       end     end   end 

i use action javascript ajax , works perfectly. try test action piece of code

  test "should create user"     assert_difference('user.count')       post "/users.json", user: { email: @user.email, name: @user.name }     end          end 

that throws

1) error: userscontrollertest#test_should_create_user: actioncontroller::urlgenerationerror: no route matches {:action=>"/users.json", :controller=>"users", :user=>{:email=>"mystring", :name=>"mystring"}} test/controllers/users_controller_test.rb:16:in 'block (2 levels) in ' test/controllers/users_controller_test.rb:15:in 'block in '

so question is, how can test action respond json format? , why path /users.json works in app isn't recognized when try test it?

this how can create json request

post :create, { user: { email: @user.email, name: @user.name }, format: :json } 

this how can test response type

rspec

expect(response.content_type).to eq("application/json") 

minitest

assert_includes @response['content-type'], 'application/json' 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -