3

rspec コードで次のエラーが発生します

undefined local variable or method `render'

ここに私のコードがあります:

require 'spec_helper'
describe "messages/show.html.erb" do
   it "displays the text attribute of the message" do
    render
    rendered.should contain("Hello world!")
  end
end

これは本からのものです: 以下の RSpec の本は、私が Gemfile に追加したものです。

group :development , :test do
   gem "rspec-rails", ">= 2.0.0"
   gem "webrat", ">= 0.7.2"
end

ファイル show.html.erb をビュー/メッセージに追加していませんが、これではなく別のエラーが発生するはずです

奇妙なことは、これが私のマシンで以前に機能したことです。プロジェクトを削除して別のプロジェクトを作成しましたが、機能しなくなりました

gemfileを編集した後にこれらのコマンドを発行しました

bundle install
script/rails generate rspec:install
rake db:migrate
4

4 に答える 4

7

私にとっての答えは変わることでした

describe "messages/show.html.erb" do

describe "messages/show.html.erb", type: :view do
于 2015-07-16T03:15:06.527 に答える
5

show.html.erb_spec.rb ファイルに以下が含まれている場合:

require 'spec_helper.rb'

次のように変更してみてください。

require 'rails_helper.rb'

説明: https://www.relishapp.com/rspec/rspec-rails/docs/upgrade

于 2014-10-22T02:24:04.693 に答える
0

次のようにテストを変更できます。

require 'spec_helper'
describe "messages/show.html.erb" do
  it "displays the text attribute of the message" do
    FactoryGirl.create(:message) # or an other test data creator gem
    visit "/messages/1"
    page.should have_content("Hello world!")
  end
end
于 2013-04-26T14:45:50.940 に答える