ruby on rails - rspec controller testing with devise -
i'm using devise + rspec + factory + shoulda , having trouble controller specs. i've read bunch of articles , docs couldn't figure out best way log_in
user , use user instance.
task nested under user index route /users/:user_id/tasks
, task belongs_to :assigner, class_name: "user"
, belongs_to :executor, class_name: "user"
at moment following code both tests fail. best approach sign_in user , use in controller tests?
the error message first one:
failure/error: expect(assigns(:tasks)).to eq([assigned_task, executed_task]) expected: [#<task id: 1, assigner_id: 1, executor_id: 2, .....>, #<task id: 2, assigner_id: 3, executor_id: 1, ......>] got: nil (compared using ==)
the error second one:
failure/error: { is_expected.to respond_with :ok } expected response 200, 302
tasks_controller_spec.rb
require "rails_helper" describe taskscontroller describe "when user signed in" describe "collections" login_user let(:assigned_task) { create(:task, assigner: @user) } let(:executed_task) { create(:task, executor: @user) } let(:other_task) { create(:task) } context "get index" before :index, user_id: @user.id end "assigns user's tasks" expect(assigns(:tasks)).to eq([assigned_task, executed_task]) end { is_expected.to respond_with :ok } end context "get incoming_tasks" end end end
controller_macros.rb
module controllermacros def login_user before(:each) @request.env["devise.mapping"] = devise.mappings[:user] @user = create(:user) sign_in @user end end end
tasks controller
def index @tasks = task.alltasks(current_user).uncompleted.includes(:executor, :assigner).order("deadline desc").paginate(page: params[:page], per_page: task.pagination_per_page) end
add following line in rails_helper.
config.include controllermacros, :type => :controller
see thread.
i assuming fails in rspec. when test in browser works fine.
Comments
Post a Comment