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
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.
i think not calling
create
actionexpect(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
is controller working expected?
format.js
returning string (flash.now[:success]
orflash.now[:alert]
instead of javascript code. perhaps should useformat.text
, or return nothingrender status: :created, nothing: true
.
Comments
Post a Comment