Ruby on rails:未定义的方法'let'

时间:2014-04-06 04:24:38

标签: ruby-on-rails rspec

我是Ruby on Rails的新手,当我尝试使用static_pages_specs.rb中的方法时


   require `spec_helper` 
   describe "Static pages" do

      let(:base_title) { "Ruby on Rails Tutorial Sample App" }

      describe "Home page" do
        it "should have the title 'Home'" do
          visit '/static_pages/home'
          expect(page).to have_title("#{base_title} | Home")
        end
      end

    end

我收到错误

    Failure/Error: visit '/static_pages/home'
         ActionView::Template::Error:
           undefined method `let' for Class:0x000000034b2230>:0x0000000374a6c8>

我的Gemfile看起来像这样



    source 'https://rubygems.org'
    ruby '1.9.3'

    gem 'rails', '4.0.4'

    group :development, :test do
        gem 'sqlite3'
        gem 'rspec-rails'
    end

    group :test do
        gem 'selenium-webdriver'
        gem 'capybara'
    end 

    gem 'sass-rails', '~> 4.0.2'
    gem 'uglifier', '>= 1.3.0'
    gem 'coffee-rails', '~> 4.0.0'
    gem 'jquery-rails'
    gem 'turbolinks'
    gem 'jbuilder', '~> 1.2'

    group :doc do
    # bundle exec rake doc:rails generates the API under doc/api.
       gem 'sdoc', require: false
    end

    group :production do
       gem 'pg', '0.15.1'
       gem 'rails_12factor', '0.0.2'
    end

    

另外我的spec_helper.rb看起来像这样



      ENV["RAILS_ENV"] ||= 'test'
      require File.expand_path("../../config/environment", __FILE__)
      require 'rspec/rails'
      require 'rspec/autorun'

      Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

    # If you are not using ActiveRecord, you can remove this line.
      ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

      RSpec.configure do |config|

    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
      config.fixture_path = "#{::Rails.root}/spec/fixtures"

    # If you're not using ActiveRecord, or you'd prefer not to run each of your
    # examples within a transaction, remove the following line or assign false
    # instead of true.
      config.use_transactional_fixtures = true

    # If true, the base class of anonymous controllers will be inferred
    # automatically. This will be the default behavior in future versions of
    # rspec-rails.
      config.infer_base_class_for_anonymous_controllers = false

      config.order = "random"
      config.include Capybara::DSL
    end

你有什么想法我错过了什么?我使用的是Ubuntu 13.04。

编辑:已经尝试过让bang方法,但它仍然会得到相同的错误。

在上一篇文章中,我忘记了

    require 'spec_helper'

in,但我的代码实际上是DID拥有它。 对此感到抱歉,但实际上并不存在问题。

解决了:谢谢你们,我刚刚解决了它,它只是在我的一个模板中愚蠢地输入。

1 个答案:

答案 0 :(得分:5)

您的static_pages_specs.rb遗失require 'spec_helper'

require `spec_helper`

describe "Static pages" do
  ...
end