Note that there are some explanatory texts on larger screens.

plurals
  1. POFactory girl not initiating object
    primarykey
    data
    text
    <p>I guess the problem is that I do not know how to use factory girl with Rspec correctly. Or testing in rails correctly for that matter. Still think it is a bit weird though..</p> <p>I have a class, User, with the following factory:</p> <pre><code>FactoryGirl.define do factory :user do name "admin" email "admin@admin.com" adminstatus "1" password "foobar" password_confirmation "foobar" end factory :user_no_admin, class: User do name "user" email "user@user.com" adminstatus "2" password "foobar" password_confirmation "foobar" end ... </code></pre> <p>My test looks like this:</p> <pre><code>... describe "signin as admin user" do before { visit login_path } describe "with valid information" do let(:user_no_admin) { FactoryGirl.create(:user_no_admin) } let(:user) { FactoryGirl.create(:user) } before do fill_in "User", with: user.name fill_in "Password", with: user.password click_button "Login" end it "should list users if user is admin" do response.should have_selector('th', content: 'Name') response.should have_selector('td', content: user_no_admin.name) response.should have_selector('td', content: user.name) end end end#signin as admin user ... </code></pre> <p>Basically I am trying to test that if you log in as an admin, you should see a list of all the users. I have a test for logging on as a non-admin later on in the file. I have a couple of users in the db already.</p> <p>In the list of users 'admin' that logged in is displayed along with the users already in the db. 'user' is however not displayed unless I do something like this before:</p> <pre><code>fill_in "User", with: user_no_admin.name fill_in "Password", with: user_no_admin.password </code></pre> <p>It is as if it won't exist unless I use it. However, if I use a puts it does print the information I am putting, even if I do not do the 'fill_in' above.</p> <p>I have a similar example where a puts helps me.</p> <pre><code>describe "should have company name" do let(:company) { FactoryGirl.create(:company) } let(:category) { FactoryGirl.create(:category) } let(:company_category) { FactoryGirl.create(:company_category, company_id: company.id, category_id: category.id) } it "should contain companies name" do puts company_category.category_id get 'categories/' + company.categories[0].id.to_s response.should have_selector('h4', :content =&gt; company.name) end end </code></pre> <p>Without the puts above I get a </p> <pre><code>Called id for nil </code></pre> <p>Do I have to initiate(?) an object created by Factory girl before I can use it in some way?</p> <p>Any other code needed?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload