我正在使用脚手架来生成rspec控制器测试.默认情况下,它会将测试创建为:
let(:valid_attributes) {
skip("Add a hash of attributes valid for your model")
}
describe "PUT update" do
describe "with valid params" do
let(:new_attributes) {
skip("Add a hash of attributes valid for your model")
}
it "updates the requested doctor" do
company = Company.create! valid_attributes
put :update, {:id => company.to_param, :company => new_attributes}, valid_session
company.reload
skip("Add assertions for updated state")
end
Run Code Online (Sandbox Code Playgroud)
使用FactoryGirl,我已经填写了:
let(:valid_attributes) { FactoryGirl.build(:company).attributes.symbolize_keys }
describe "PUT update" do
describe "with valid params" do
let(:new_attributes) { FactoryGirl.build(:company, name: 'New Name').attributes.symbolize_keys …Run Code Online (Sandbox Code Playgroud) 我想在控制器测试中使用FactoryGirl.attributes_for,如:
it "raise error creating a new PremiseGroup for this user" do
expect {
post :create, {:premise_group => FactoryGirl.attributes_for(:premise_group)}
}.to raise_error(CanCan::AccessDenied)
end
Run Code Online (Sandbox Code Playgroud)
...但这不起作用,因为#attributes_for省略了:user_id属性.这里的区别#create和#attributes_for:
>> FactoryGirl.create(:premise_group)
=> #<PremiseGroup id: 3, name: "PremiseGroup_4", user_id: 6, is_visible: false, is_open: false)
>> FactoryGirl.attributes_for(:premise_group)
=> {:name=>"PremiseGroup_5", :is_visible=>false, :is_open=>false}
Run Code Online (Sandbox Code Playgroud)
请注意:不存在:user_id #attributes_for.这是预期的行为吗?
FWIW,我的工厂文件包含的定义:premise_group和:user:
FactoryGirl.define do
...
factory :premise_group do
sequence(:name) {|n| "PremiseGroup_#{n}"}
user
is_visible false
is_open false
end
factory :user do
...
end
end
Run Code Online (Sandbox Code Playgroud) 有以下工厂:
factory :car do
name 'Some car'
engine_value 1.6
color '#ff0000'
car_type
engine_type
transmission
drive_type
material
end
Run Code Online (Sandbox Code Playgroud)
如你所见,有很多相关的对象.但是代码
attributes_for(:car)
Run Code Online (Sandbox Code Playgroud)
只生成:name=>"Some car", :engine_value=>1.6, :color=>"#ff0000"}哈希.我需要获得具有所有属性的哈希.我该怎么做?谢谢.
我正试图在我的Rspec控制器测试中测试关联.问题是Factory不会为attributes_for命令生成关联.所以,按照这篇文章中的建议,我在我的控制器规范中定义了我的验证属性,如下所示:
def valid_attributes
user = FactoryGirl.create(:user)
country = FactoryGirl.create(:country)
valid_attributes = FactoryGirl.build(:entitlement, user_id: user.id, country_id: country.id, client: true).attributes.symbolize_keys
puts valid_attributes
end
Run Code Online (Sandbox Code Playgroud)
但是,当控制器测试运行时,我仍然会收到以下错误:
EntitlementsController PUT update with valid params assigns the requested entitlement as @entitlement
Failure/Error: entitlement = Entitlement.create! valid_attributes
ActiveRecord::RecordInvalid:
Validation failed: User can't be blank, Country can't be blank, Client & expert are both FALSE. Please specify either a client or expert relationship, not both
Run Code Online (Sandbox Code Playgroud)
然而,终端中的valid_attributes输出清楚地显示每个valid_attribute都有user_id,country_id和expert设置为true:
{:id=>nil, :user_id=>2, :country_id=>1, :client=>true, :expert=>false, :created_at=>nil, :updated_at=>nil}
Run Code Online (Sandbox Code Playgroud)