试图习惯 rspec let / subject 语法,但我无法弄清楚。我有这个规格:
describe Permission do
subject(:permission){FactoryGirl.create(:permission, role: role, branch: branch, user: user)}
let(:role){FactoryGirl.create(:role, name: 'master')}
let(:branch){FactoryGirl.create(:branch)}
let(:user){FactoryGirl.create(:user)}
context "role_names_by_user_and_branch" do
it "should return role names for the given user / branch pair" do
# permission.save
Permission.role_names_by_user_and_branch(user, branch).should include(role.name)
end
end
end
注意注释掉的permission.save
。如果我在测试通过中添加它。这是我不明白的部分。如果我这样做FactoryGirl.create
,应该已经保存了该权限,并且查询工作。
我的工厂非常基本:
FactoryGirl.define do
factory :permission do
user
branch
role
end
end