[英]RSpec and weird tests results
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.
但是,当我尝试使用RSpec(2.5)运行一些测试时,在:create 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 website_controller_spec.rb:34是指创建方法
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. 任何对此的想法将不胜感激。
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 the list
action. 如果我理解正确,当您传递空白的
:adres
属性时,保存将失败并且页面将呈现list
动作。 So you want to stub the create
method and return true or false depending on the expected result: 因此,您想存根
create
方法并根据预期结果返回true或false:
it "succeeds" do
@website = mock_model(Website,:save=>true)
Website.stub(:new) { @website }
post :create, :website => {}
# redirects
response.should have_selector("etc etc")
end
it "fails" do
@website = mock_model(Website,:save=>false)
Website.stub(:new) { @website }
post :create, :website => {}
# renders 'new'
response.should_not have_selector("etc etc")
end
Testing of the validity of the parameters should be performed in the model spec: 参数有效性的测试应在模型规格中执行:
@website = Website.new(:adres=>"")
@website.should_not be_valid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.