使用rspec集成测试新记录

时间:2012-04-25 18:39:04

标签: ruby-on-rails testing rspec

我正在测试项目的创建(使用rspec集成测试),我想知道为什么测试不会找到我刚刚告诉它创建的项目...这是我的代码

  1 require 'spec_helper'
  2 
  3 describe "Projects" do
  4   describe "create project" do
  5 
  6     before :each do
  8       @valid_project = { :name => 'Myproject', :description => 'project description' }
  9     end
 10 
 11     it "should create and redirect to a new project" do
 12       lambda do
 13         visit root_path
 14         click_link 'new project'        
 15         fill_in :name, :with => @valid_project[:name]
 16         fill_in :description, :with => @valid_project[:description] 
 17         click_button 'Create'
 18         current_path.should == project_path(Project.find_by_name!(@valid_project[:name]))
 19         page.should have_content 'Project has been created'
 20 
 21         #in adition, all these don't work and I don't know why...
 22 
 23         #response.should render_template :new
 24         #page.should have_content 'Myproject'
 25         #response.should have_selector('h1', :content => 'Myproject')
 26 
 27       end.should change(Project, :count).by(1)
 28     end
 29   end
 30 end

第18行返回此错误:

  

ActiveRecord :: RecordNotFound:找不到名为=的Project   MYPROJECT

我不明白为什么......在上面代码的评论中还有一些不起作用的测试,而且,我不知道为什么......谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

这可能意味着“创建”按钮操作以某种方式失败,因此未创建记录。

尝试插入

save_and_open_page

click_button 'Create'

这将在浏览器中打开页面并显示页面的当前状态。