[英]Rails 3.1 Rspec test fails in Posts Controller “POST 'create'”
I have a test that fails and I just cannot figure out how to get it working. 我的测试失败了,但我无法弄清楚如何使其正常工作。 It's the it "should have the right title" test in the "POST Create" section.
它是“ POST Create”部分中的“应该具有正确的标题”测试。 It's a Rails 3.1 app, heres my gem file for testing:
这是一个Rails 3.1应用程序,这里是我的gem文件,用于测试:
group :test do
gem 'turn', :require => false
gem 'minitest'
gem 'spork', '> 0.9.0.rc'
gem 'guard-spork'
gem 'guard-rspec'
gem 'webrat'
gem 'factory_girl_rails', '> 1.0'
gem 'mocha'
gem 'rspec-rails'
end
My Posts Controller 我的帖子控制器
class PostsController < ApplicationController
def new
@post = Post.new
@title = "Create a new post"
respond_to do |format|
format.html # new.html.erb
format.json { render json: @post }
end
end
def show
@post = Post.find(params[:id])
@title = @post.title
respond_to do |format|
format.html # show.html.erb
format.json { render json: @post }
end
end
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render json: @post, status: :created, location: @post }
else
format.html { render action: "new" }
format.json { render json: @post.errors, status: :unprocessable_entity }
@title = "Create a new post"
end
end
end
end
Rspec tests - I have added render_views to file. Rspec测试-我已将render_views添加到文件中。
describe "POST 'create'" do
describe "SUCCESS" do
before(:each) do
@attr = { :title => "Foo", :content => "Bar" }
end
it "should create a new post" do
lambda do
post :create, :post => @attr
flash[:notice].should_not be_nil
end.should change(Post, :count).by(1)
end
it "should redirect to the post show page and show success message" do
post :create, :post => @attr
flash[:notice].should =~ /post was successfully created/i
response.should redirect_to post_path(assigns(:post))
end
it "should have the right title" do
post :create, :post => @attr
response.should have_selector("title", :content => assigns(:post).title)
end
end
end
Only the last test fails. 只有最后一次测试失败。 Error messages:
错误讯息:
Finished in 6.49 seconds
11 examples, 1 failure
Failed examples:
rspec ./spec/controllers/posts_controller_spec.rb:62 # PostsController POST 'create' SUCCESS should have the right title
Running: spec/controllers/posts_controller_spec.rb
.......F...
Failures:
1) PostsController POST 'create' SUCCESS should have the right title
Failure/Error: response.should have_selector("title", :content => assigns(:post).title)
expected following output to contain a <title>Foo</title> tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>You are being <a href="http://test.host/posts/980190963">redirected</a>.</body></html>
# ./spec/controllers/posts_controller_spec.rb:64:in `block (4 levels) in <top (required)>'
Any help as to why this fails would be greatly appreciated. 对于为什么失败的任何帮助将不胜感激。
You're willing something that simply won't happen. 您愿意做一些根本不会发生的事情。
Because you redirect, the response is: 由于您重定向,因此响应为:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>You are being <a href="http://test.host/posts/980190963">redirected</a>.</body></html>
It would be better to test the new object itself. 最好测试新对象本身。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.