ruby - Testing Create method from controller -


i looking test create method in controller, checking validations , flash messages set correctly. have far is

it 'is invalid invalid formatted email'   @message = message.new(factorygirl.attributes_for(:message_invalid_email))   @message.valid?   expect(@message.errors[:email].first).to eq("don't forget add email address") end 

but have seen others setup test like

@message = post :create, message: { params1: value1 } 

what difference here , how should testing this?

and when try , test flash success has been set (using shoulda gem)

it 'is valid correct parameters'   @message = message.new(factorygirl.attributes_for(:valid_message))   expect(@message).to be_valid   expect(controller).to set_flash[:success] end 

i error

expected flash[:success] set, no flash set 

this controller

def create   @message = message.new(message_params)   respond_to |format|     if @message.valid?       format.js { flash.now[:success] = "thanks message, i'll in touch soon" }     else       format.js { flash.now[:alert] }     end   end end 

any advice/help appreciated

thanks

  1. the difference between testing method , other that, in first case, testing model, and, in second case, testing controller. also, validations should tested on model; flash messages , actions behaviour, within controller.

  2. i think not calling create action expect(controller).to set_flash[:success]. perhaps should try following block:

    it 'is valid correct parameters'   post :create, message: { params1: value1 } # whatever post action   expect(flash.now[:success].now).to be_present end 
  3. is controller working expected? format.js returning string (flash.now[:success] or flash.now[:alert] instead of javascript code. perhaps should use format.text, or return nothing render status: :created, nothing: true.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -