我如何在这里避免hardcoding
身份证?下面是我正在硬编码id 22的代码。
it "can find an Project that this user belongs to" do
project = Project.find( id: '22', authorization: @auth )
hashed_response = FactoryGirl.build(:project_json).marshal_dump.to_json
expect(project.to_json).to eq(hashed_response);
end
以下是完整课程的代码。
require 'spec_helper'
describe Project do
before(:all) do
@project = FactoryGirl.create(:project, name: "123", description: "123");
@user = FactoryGirl.create(:user, email: "test@test.com", password: nil, code: nil);
user = User.login(FactoryGirl.create(:user, email: "test@test.com",
password: "123", code: 0));
if user
@auth = user['auth']
end
end
it "can create an Project" do
expect{Project.create(project: @acc, user: @user)}.to
change{Project.all(authorization: @auth).size}.by (1);
end
it "can find an Project that this user belongs to" do
project = Project.find( id: '22', authorization: @auth )
hashed_response = FactoryGirl.build(:project_json).marshal_dump.to_json
expect(project.to_json).to eq(hashed_response);
end
end
我正在查看一些教程http://everydayrails.com/2012/04/07/testing-series-rspec-controllers.html,但我不明白它们是如何传递的。
答案 0 :(得分:2)
如果项目属于用户,则可以执行...
project = Project.find_by( user_id: @user.id , authorization: @auth )
如果用户可以属于某个项目,那么你就可以......
project = Project.find_by( id: @user.project_id, authorization: @auth )
答案 1 :(得分:1)
项目ID是如何提供的?用表格?那么它将是params [:id]
或params [:project_id]
,但在测试中它将是@ project.id