lundi 25 avril 2016

rspec controller spec for js request

In my controller specs everything works fine (update action, edit action, etc. also via js request), except the create action. For some reason it doesn't change the Task.count, but the http response is 200.

There are no other callbacks and in dev ENV it saves the task in the db. This factory passes in model spec. I also tried to comment out the Notification.create, TaskCreatorJob and Conversation.create.., but didn't help. In my other controller specs the expect { create_action }.to change{Class.count}.by(1) works properly.

What did I miss?

conroller

def create
  @task = Task.new(task_params)
  @task.assigner_id = current_user.id
  if @task.save
    Notification.create(recipient_id: @task.executor_id, sender_id: current_user.id, notifiable: @task, action: "assigned")
    TaskCreatorJob.perform_later(@task, @task.executor, @task.assigner)
    Conversation.create_or_find_conversation(@task.assigner_id, @task.executor_id)
    respond_to do |format|
      format.js
    end     
  else
    respond_to do |format|
      format.js
    end
  end
end

factory

factory :task do
  content { Faker::Lorem.sentence }
  deadline { Faker::Time.between(DateTime.now + 2, DateTime.now + 3) }
  association :executor, factory: :user
  association :assigner, factory: :user
end

tasks_controller_spec.rb

.....
before(:each) do
    login_user
end

describe "POST create" do

  context "with valid attributes" do
    let!(:user) { create(:user) }
    let!(:profile) { create(:profile, user: @user) }
    let!(:profile_2) { create(:profile, user: user) }
    let!(:conversation) { create(:conversation, sender: @user, recipient: user) }
    subject(:create_action) { xhr :post, :create, user_id: @user.id, task: attributes_for(:task, assigner: @user, executor: user) }

    it "saves the new task in the db" do
      expect{ create_action }.to change{ Task.count }.by(1)
    end

    it "responds with success" do
      create_action
      expect(response).to have_http_status(200)
    end
  end
end

Aucun commentaire:

Enregistrer un commentaire