我是RSpec的新手,我不断遇到问题,那就是了解记录是在哪个范围内/哪一个范围内。见下面的例子。我通过FactoryGirl创建了一系列记录。例如,我创建了一个用户,然后在前面的块中用该用户登录。这似乎总是工作正常。但是,无论何时我开始尝试使用之前创建的记录的子记录,我似乎都会陷入困境。例如与组。组需要一个用户和一个group_type。每当我尝试用这些记录做某件事时,我都会遇到“被调用的ID为零”错误。我试过“puts group.name”,我可以看到该组正在创建,但由于某些原因,“访问group_path(group)”行执行该记录不再存在。我知道我在这里错过了一些非常重要的东西。感谢任何帮助,因为这一直使我疯狂。RSpec在请求规范中引用子记录规范
describe "activities page" do
let(:user) { FactoryGirl.create(:user) }
let!(:group_type) { FactoryGirl.create(:group_type) }
let!(:group) { FactoryGirl.create(:group, user: user, group_type: group_type) }
before do
sign_in user
visit group_path(group)
click_button "Check In"
end
错误:
1) Activity pages activities page
Failure/Error: visit group_path(group)
RuntimeError:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
添加组显示方法:
def show
@group = Group.find(params[:id])
@members = @group.members.paginate(page: params[:page])
cookies[:group_id] = @group.id
# Set Pusher chat
@chat = Chat.find_by_group_id(@group.id)
@user = current_user
@messages = Message.find(:all, :conditions => ["chat_id = ?", @chat.id.to_s])
end
你的GroupsController中的show方法是什么样的? – 2012-04-27 05:57:40
已在上面添加。谢谢参观。 – pejmanjohn 2012-04-27 17:05:30
是@ @ chat'零?您的测试似乎没有创建与其创建的组相关的任何聊天。 – 2012-04-27 17:43:23