我想問題是我不知道如何正確使用工廠女孩與Rspec。或者正確測試軌道。還是覺得雖然有點怪異..工廠女孩不啓動對象
我有一個類,用戶,用下面的工廠:
FactoryGirl.define do
factory :user do
name "admin"
email "[email protected]"
adminstatus "1"
password "foobar"
password_confirmation "foobar"
end
factory :user_no_admin, class: User do
name "user"
email "[email protected]"
adminstatus "2"
password "foobar"
password_confirmation "foobar"
end
...
我的測試是這樣的:
...
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
...
基本上我想要測試如果您以管理員身份登錄,您應該會看到所有用戶的列表。我有一個測試,以稍後在文件中以非管理員身份登錄。我已經有幾個db用戶了。
在已登錄的用戶'管理員'列表中顯示以及已經在數據庫中的用戶。然而,'用戶'是不會顯示的,除非我之前做這樣的事情:
fill_in "User", with: user_no_admin.name
fill_in "Password", with: user_no_admin.password
這就好像它不會存在,除非我使用它。但是,如果我使用提示,它會打印我所提供的信息,即使我不執行上面的'fill_in'。
我有一個類似的例子,在這個例子中,put可以幫助我。
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 => company.name)
end
end
上面沒有我的看跌期權獲得
Called id for nil
我必須啓動(?)由工廠女孩創建之前,我可以以某種方式使用它的對象?
需要其他代碼嗎?
有一天我會擅長測試,然後我的生活會很棒。謝謝阿德里安! – abegbg