ruby - Rails / RSpec factory girl testing with invalid model produces strange behaviour -


i testing rails controller rspec , factory_girl

my actions returns error messages in json format 412 status in case of invalid attributes

format.json{render json: user.errors.messages, status: 412} 

like

when trying test functionality

let(:invalid_attributes){     factorygirl.attributes_for(:user,password: "password") } "returns error if invalid attributes"   post :create,{user: invalid_attributes},valid_session   expect(response).to have_status(412) end 

i error

 failure/error: expect(response).to have_status(412)        expected #<actioncontroller::testresponse:0x00000006f4cc10> respond `has_status?` 

if try test

it "returns error if invalid attributes"     user = factorygirl.build(:user,invalid_attributes)     post :create,{user: invalid_attributes},valid_session     expect(response.body).to eq(user.errors.messages.to_json) end 

i error

 failure/error: expect(response.body).to eq(user.errors.messages.to_json)         expected: "{}"             got: "{\"password_confirmation\":[\"doesn't match password\",\"doesn't match password\"],\"password\":[\"password must contain 1 uppercase alphabet 1 lowercase alphabet 1 digit , minimum 8 charecters\"]}" 

what problem?thank in advance

you can test response status this:

expect(response.status).to eq(412) 

and json response with:

expect(response.body).to eq "{\"password_confirmation\":[\"doesn't match password\",\"doesn't match password\"],\"password\":[\"password must contain 1 uppercase alphabet 1 lowercase alphabet 1 digit , minimum 8 charecters\"]}" 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -