我正在使用Rspec和FactoryGirl为我的代码编写测试 - 其中用户has_one组织,组织has_many用户。组织名称设置为唯一,我正在愉快地使用关联:FactoryGirl的组织(在我的用户工厂内)功能测试用户(错误用户测试的一堆被黑客攻击的特征。它到达了这个没有的阶段&#39足够(我需要组织内的一些用户集合进行某些测试,而上述设置无法正常工作)所以我现在正试图在我的组织工厂内构建用户集合。这适用于某些测试但不适用于其他测试,我认为我在测试中没有正确使用创建的用户。
工厂代码:
factory :organization do
organization_name "New Example Org"
before(:create) do |organization|
organization.users << FactoryGirl.build(:user)
organization.users << FactoryGirl.build(:user, email: "email2@example.com")
end
trait :wrong do
organization_name "Wrong Org"
before(:create) do |organization|
organization.users << FactoryGirl.build(:user, email: "email3@example.com")
end
end
end
测试代码失败:
describe "as wrong user" do
let(:organization) { FactoryGirl.create(:organization, :wrong)}
let(:user) {organization.users.first}
let(:wrong_user) { organization.users.last }
before { sign_in user, no_capybara: true }
describe "submitting a GET request to the Users#show action", type: :request do
before { get user_path(wrong_user) }
specify { expect(response.body).not_to match(full_title(user.name)) }
specify { expect(response).to redirect_to(root_path) }
end
describe "submitting a GET request to the Organizations#show action", type: :request do
before { get organization_path(wrong_user.organization) }
specify { expect(response.body).not_to match(full_title(wrong_user.organization.organization_name)) }
specify { expect(response).to redirect_to(root_path) }
end
end
结果;
26) Authentication authorization as wrong user submitting a GET request to the Organizations#show action should redirect to "/"
Failure/Error: specify { expect(response).to redirect_to(root_path) }
Expected response to be a <redirect>, but was <200>
# ./spec/requests/authentication_pages_spec.rb:109:in `block (5 levels) in <top (required)>'
25) Authentication authorization as wrong user submitting a GET request to the Organizations#show action should not match "Title | Wrong Org"
Failure/Error: specify { expect(response.body).not_to match(full_title(wrong_user.organization.organization_name)) }
我在这里做错了什么?
答案 0 :(得分:0)
我最终设法解决了这个问题。问题是我只能调用一个工厂&#34; Organization&#34;,因此我在该工厂上放置的任何后续特征或构建都与同一个Organization_id相关联。修复方法如下:
工厂代码:
factory :organization do
trait :name_one do
organization_name "New Example Org"
end
trait :name_two do
organization_name "Any Wrong Org"
end
trait :with_users do
before(:create) do |organization|
organization.users << FactoryGirl.build(:user)
organization.users << FactoryGirl.build(:user, name: "Wrong Name", email: "email2@example.com")
end
end
trait :wrong do
before(:create) do |organization|
organization.users << FactoryGirl.build(:user, name: "another wrong name", email: "email3@example.com")
end
end
end
factory :user do
name "Example Name"
email "email@example.com"
password "foobar"
password_confirmation "foobar"
trait :as_admin do
admin true
end
end
end
测试代码:
describe "as wrong user" do
let(:organization) { FactoryGirl.create(:organization, :name_one, :with_users)}
let(:user) {organization.users.first}
let(:wrong_org) { FactoryGirl.create(:organization, :name_two, :wrong)}
let(:wrong_user) {wrong_org.users.last}
before { sign_in user, no_capybara: true }
describe "submitting a GET request to the Users#edit action", type: :request do
before { get edit_user_path(wrong_user) }
specify { expect(response.body).not_to match(full_title('Edit user')) }
specify { expect(response).to redirect_to(root_path) }
end
describe "submitting a PATCH request to the Users#update action", type: :request do
before { patch user_path(wrong_user) }
specify { expect(response).to redirect_to(root_path) }
end
describe "submitting a GET request to the Users#show action", type: :request do
before { get user_path(wrong_user) }
specify { expect(response.body).not_to match(full_title(user.name)) }
specify { expect(response).to redirect_to(root_path) }
end
describe "submitting a GET request to the Organizations#show action", type: :request do
before { get organization_path(wrong_user.organization) }
specify { expect(response.body).not_to match(full_title(wrong_user.organization.organization_name)) }
specify { expect(response).to redirect_to(root_path) }
end
end
describe "as non-admin user" do
let(:organization) { FactoryGirl.create(:organization, :name_one, :with_users)}
let(:non_admin) {organization.users.first}
let(:user) {organization.users.last}
before { sign_in non_admin, no_capybara: true }
describe "submitting a DELETE request to the Users#destroy action", type: :request do
before { delete user_path(user) }
specify { expect(response).to redirect_to(root_path) }
end
end
end
所有测试现在都是绿色的 - 所以通过将组织名称放入特定的特征(而不是作为工厂的一般部分),我能够创建两个不同的组织并为每个组织添加用户。希望它可以帮助别人。