Reputation: 125
I try to test my controller with Rspec:
let(:valid_attributes) {
FactoryGirl.attributes_for(:job)
}
....
describe "GET #new" do
it "assings a new job" do
get :new
expect(assigns(:job)).to be_a_new(Job)
end
end
my factories:
FactoryGirl.define do
factory :job do
title "MyString"
description "MyDescription"
city "MyCity"
date Date.today
job_end false
user
end
end
schema.rb:
create_table "jobs", force: :cascade do |t|
t.string "title"
t.text "description"
t.string "city"
t.datetime "date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.boolean "job_end"
end
and an error that I got:
Failure/Error: expect(assigns(:job)).to be_a_new(Job)
expected nil to be a new Job(id: integer, title: string, description: text, city: string, date: datetime, created_at: datetime, updated_at: datetime, user_id: integer, job_end: boolean)
I understand that my Job object which create is wrong? Or what? Can you explain me a little bit?
my controller:
def new
@job = current_user.jobs_given.build
end
def create
@job = current_user.jobs_given.build(job_params)
if @job.save
redirect_to @job
else
render 'new'
end
end
Upvotes: 1
Views: 2290
Reputation: 865
It happens because device gem redirect your to sign_in_path, because you sign in your user incorrect. You should do this at the top of your controller test:
before(:each) do
request.env["devise.mapping"] = Devise.mappings[:user]
sign_in FactoryGirl.create(:user)
end
And better way is create sign in method and call it in every controller. See here: https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-%28and-RSpec%29 (Controllers spec section)
Upvotes: 3