【发布时间】:2011-03-31 12:50:42
【问题描述】:
我正在尝试制作一个简单的应用程序。当我在浏览器中测试它时,一切都很好。但是,当我尝试使用 RSpec (2.5) 运行一些测试时,它会失败:为控制器创建测试。
这是我的创建方法:
def create
@website = Website.new(params[:website])
if @website.save
flash[:notice] = "Website created."
redirect_to(:action => 'list')
else
render('new')
end
end
控制器测试:
describe WebsitesController do
render_views
.
.
.
describe "POST 'create'" do
before(:each) do
@attr = { :adres => "www.excc.pl", :opis => "aaa "*22, :tagi => "aaa aaa aaa",
:preview => File.new(Rails.root + 'spec/fixtures/rails.png'),
:preview_mini => File.new(Rails.root + 'spec/fixtures/rails.png')}
end
describe "success" do
it "should have the right title" do
response.should have_selector("title", :content=>"Lista witryn w portfolio")
end
end
.
.
.
本次测试结果:
1) WebsitesController POST 'create' should have the right title
Failure/Error: response.should have_selector("title", :content=>"Lista witryn w portfolio")
expected following output to contain a <title>Lista witryn w portfolio</title> tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# ./spec/controllers/websites_controller_spec.rb:34:in `block (4 levels) in
websites_controller_spec.rb:34 指创建方法
但是,此测试正确通过(对于不正确的数据,应将其重定向回具有指定标题的“新”站点):
it "should have the right title" do
post :create, :website => @attr.merge(:adres => "")
response.should have_selector("title", :content=>"Dodaj stronę WWW")
end
第二个问题是…… 曾经有一段时间我得到了这样的测试结果:
<html><body>You are being <a href="http://test.host/websites/list">redire cted</a>.</body></html>
...这导致我把头发拉了一段时间,直到我做了某事(我真的不知道是什么)并且它消失了。然而,当我想到它将来会回来毁掉我的幸福时,它让我害怕得要命。
对此的任何想法将不胜感激。
【问题讨论】:
标签: ruby-on-rails-3 testing rspec rspec2