ruby on rails - How to create a model with a certain id using rspec and factory girl -


i use:

gem 'rails', '3.2.11' gem 'rspec-rails', '2.13.2' gem 'webrat', '0.7.3' gem 'factory_girl_rails', '4.1.0' gem 'spork', '~> 0.9.0.rc' 

i want test hp have link user, pages controller hp contains:

@user = user.find(7) 

and view hp contains:

link_to 'see user', @user 

the problem tests fail since test database has no user id 7. tried:

factorygirl.define   factory :user     name "testuser"     id "7"   end end 

... doesn't work. there error:

the spec this:

describe "get 'home'"      before(:each)       factorygirl.create(:user)     end      "should successful"       'home'       response.should be_success     end end 

failure/error: 'home' activerecord::recordnotfound: couldn't find user id=7

the hp working fine in reality, test fails. how can assure test not going fail?

what mocking / stubbing (much faster spec when not saving objects database):

let(:user) { factorygirl.build(:user) } let(:id) { '1' }  before   user.stub(:id).and_return(id) # illustrate, not necessary if stubbing find class method   user.stub(:find).and_return(user) end  { expect(user.id).to eq(id) } # illustrate { expect(response).to be_success } { expect(assigns(:user)).to eq(user) } 

if still have issue user being instantiated, can still use above technique replace factorygirl.build(:user) factorygirl.create(:user)


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -