ruby on rails - Why controller spec is fail? -


i write spec controller action create , when running test, shell show me error

   expected #count have changed 1, changed 0 

there spec

let(:valid_attributes) { factorygirl.attributes_for(:company) }  describe 'with valid params'   'creates new company'     expect { post :create, company: valid_attributes }.to change(company, :count).by(1) 

and there factory

factorygirl.define    factory :company     title faker::company.name     url faker::internet.domain_word     image file.open(file.join(rails.root, '/spec/fixtures/poeox311zom.jpg'))   end end 

how fix? don't know did wrong

upd

  def create     @company = company.new(company_params)     if @company.save       redirect_to root_path     else       redirect_to root_path     end   end 

the difference between @object.save , @object.save! first fails softly returning false, , why if @object.save because if return true if saved , false otherwise. second (the bang! method) throws error, more sutable conditions don't check return value, or maybe rake task.

also controller should handle failing saves rather redirect, example:

def create   @company = company.new(company_params)   if @company.save     redirect_to root_path   else     render :new   end end 

this way failing object carried along :new template, , there handle errors, this

<% if @company.errors %>   <% @company.errors.full_messages.each |message| %>     <div class='error'><%= message %></div>   <% end %> <% end %> 

this way template show error


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -