Rspec rendering text

2019-04-21 14:44发布

问题:

I have this Code

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

and I try verify with rspec the render "OK"

this is my actual spec:

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

but this spec respond 0 failures always, even when I put "OKI" in place "OK"

anyone have one suggestion for that?

回答1:

If you are using rails 3 or above

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

will work



回答2:

response.body.should == "OK"

works for me



回答3:

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