RSpec 和奇怪的测试结果
我正在尝试制作一个简单的应用程序。当我在浏览器中测试它时,一切正常。然而,当我尝试使用 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>
……这让我抓狂了一段时间,直到我做了某件事(我真的不知道是什么),然后它就消失了。然而,当我想到它将来可能会再次出现并毁掉我的幸福时,我感到非常害怕。
任何对此的想法将不胜感激。
I'm trying to make a simple app. When Im testing it in browser everytyhing works just fine. Howerver, when I try to run some tests with RSpec (2.5) it fails when it comes to :create test for controller.
Here's my create method:
def create
@website = Website.new(params[:website])
if @website.save
flash[:notice] = "Website created."
redirect_to(:action => 'list')
else
render('new')
end
end
The controller test:
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
.
.
.
The result of this test:
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 refers to create method
However, this test is passed correctly (for incorrect data it should be redirected back to 'new' site with specified title):
it "should have the right title" do
post :create, :website => @attr.merge(:adres => "")
response.should have_selector("title", :content=>"Dodaj stronę WWW")
end
The second problem is...
There was a time when I've got a test result like this:
<html><body>You are being <a href="http://test.host/websites/list">redire cted</a>.</body></html>
... which was causing me to pull my hair out for some time until I've done sth (I don't really know what) and it was gone. Yet, it makes me scared like hell when I think that it can come back in future an ruin my happiness.
Any thoughts on this would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很难知道这里问的是什么,但我相信问题在于你没有设定成功/失败的条件。如果我理解正确,当您传入空白
:adres
属性时,保存应该失败,并且页面应该呈现list
操作。因此,您希望对create
方法进行存根并根据预期结果返回 true 或 false:应在模型规范中执行参数有效性的测试:
It's hard to know what is being asked here, but I believe the issue is that you are not setting the conditions for success/failure. If I understand correctly, when you pass in an blank
:adres
attribute, the save should fail and the page should render thelist
action. So you want to stub thecreate
method and return true or false depending on the expected result:Testing of the validity of the parameters should be performed in the model spec: