Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Rspec 3 controller action with format.js

I'm having a bit of trouble writing rspec tests for controller actions that only respond to js.

Example Controller action:

def create
 @user = user.new(user_params)

 respond_to do |format|
   if user.save
     format.js
   end
 end
end

Here's some rspec I would write if the controller action responded to html:

context 'with valid attributes' do
  it 'creates a new user' do
    expect {
      post :create, user: build(:user)
    }.to change(User,:count).by(1)
  end

I am trying to figure out how to write a similar test but make it work with the format js.

Thanks!

like image 732
bobcobb Avatar asked Dec 15 '22 16:12

bobcobb


1 Answers

Use this xhr :post, :create, user: build(:user)

like image 108
Ashutosh Tiwari Avatar answered Dec 30 '22 08:12

Ashutosh Tiwari