2012-06-06 86 views
7

我有这样的代码Rspec的渲染文本

if @temp_user.save 
    sign_in(:user, @temp_user) 
    render text: "OK" 
else 
    render text: render_to_string(:partial => "errors") 
end 

,我尝试使用RSpec验证渲染 “OK”

这是我的实际规格:

it "render text OK" do 
    post :create, {:agent => valid_attributes} 
    # response.should have_content("OK") 
    response.should render_template(:text => "OK") 
    end 

但这个天赋响应0即使我把“OKI”置于“OK”的位置,也会出现故障

任何人都有一个建议吗?

+0

可能是 '描述 “渲染文本的确定” 做',而不是'它'呈现文字确定“做”? – Alexander

回答

3

如果您使用的轨道3或以上

expect(response.body).to eq "OK" 

将工作

2
describe "render text OK" do 
    post :create, {:agent => valid_attributes} 
    # response.should have_content("OK" 
    response.should render_template(:text => "OK") 
end 
+1

expect(响应).to render_template(:text =>'') – drhenner

+0

@ drhenner的建议结果为'Unknown key::text。有效的键是::layout,:partial,:locals,:count,:file' for me。 –

+0

我唯一的意见是使用'expect' ...你使用的是哪个版本的Rails? – drhenner

12
response.body.should == "OK" 

作品对我来说

+0

在rspec 3及以上,它应该是: 'expect(response.body).to eq“OK” – abonec