rails rspec - (第二次测试)预期的响应是<:redirect>,但是< 200> [英] rails rspec - (2nd test) Expected response to be a <:redirect>, but was <200>
本文介绍了rails rspec - (第二次测试)预期的响应是<:redirect>,但是< 200>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
预期回应为<:redirect>,但是<200>
Expected response to be a <:redirect>, but was <200>
我的测试有:
describe "Link POST #create" do
context "with valid attributes" do
it "creates a new link" do
expect{
post :create, link: FactoryGirl.create(:link, :group => @group)
}.to change(Link,:count).by(1)
end
it "redirects to the new link" do
post :create, link: FactoryGirl.create(:link, :group => @group)
# response.should redirect_to @link # Link.unscoped.last
response.should redirect_to Link.unscoped.last # render_template :show
end
end
第一个测试通过,但第二个失败。
The first test passes but the second fails.
我的代码是:
def create
@link = Link.new(params[:link])
respond_to do |format|
if @link.save
flash[:notice] = 'Link was successfully created.'
format.html { redirect_to(@link) }
format.xml { render :xml => @link, :status => :created, :location => @link }
else
@selected_group = params[:group_id]
format.html { render :action => "new" }
format.xml { render :xml => @link.errors, :status => :unprocessable_entity }
end
end
end
尝试重定向和渲染,但不能让第二个测试通过。
I've tried redirect and render but can't get the 2nd test to pass.
推荐答案
p>
Here's something that should make it work:
describe "Link POST #create" do
context "with valid attributes" do
def do_post( format = 'html' )
attributes = FactoryGirl.build(:link).attributes.merge( :group_id => @group.id )
post :create, :link => attributes, :format => 'html'
end
it "creates a new link" do
expect{
do_post
}.to change(Link,:count).by(1)
end
it "redirects to the new link" do
do_post
response.should redirect_to( assigns[:link] )
end
end
第一个规格只是因为你正在呼叫 FactoryGirl。创建,因此正在从控制器创建记录,但很可能是控制器调用无法正常工作。
The first spec was working only because you were calling FactoryGirl.create so a record was being created out of the controller, but most likely the controller call wasn't working.
这篇关于rails rspec - (第二次测试)预期的响应是<:redirect>,但是< 200>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文