Ruby on rails 3.2 简单脚手架放置:ActionView::Template::Error:未定义的方法'edit_user_path';

Ruby on rails 3.2 简单脚手架放置:ActionView::Template::Error:未定义的方法'edit_user_path';,ruby-on-rails-3.2,rspec2,erb,Ruby On Rails 3.2,Rspec2,Erb,我有一个简单的脚手架: rspec: require 'spec_helper' describe "users/show" do before(:each) do @user = assign(:user, stub_model(User, :first_name => "First Name", :last_name => "Last Name" )) end it "renders attributes in <p&

我有一个简单的脚手架:

rspec:

require 'spec_helper'

describe "users/show" do
  before(:each) do
    @user = assign(:user, stub_model(User,
      :first_name => "First Name",
      :last_name => "Last Name"
    ))
  end

  it "renders attributes in <p>" do
    render
    # Run the generator again with the --webrat flag if you want to use webrat matchers
    rendered.should match(/First Name/)
    rendered.should match(/Last Name/)
  end
end
rake routes表示用户有路由

               users GET    /users(.:format)                     users#index
                     POST   /users(.:format)                     users#create
            new_user GET    /users/new(.:format)                 users#new
           edit_user GET    /users/:id/edit(.:format)            users#edit
                user GET    /users/:id(.:format)                 users#show
                     PUT    /users/:id(.:format)                 users#update
                     DELETE /users/:id(.:format)                 users#destroy
在我运行rspec puts的任何模型上

ActionView::Template::Error: undefined method `edit_user_path' for #<#<Class:0x007fc99a517cd0>:0x007fc99a577c20>
./app/views/users/show.html.erb:14:in 

和彼得聊了一个小时后。问题在于spec_helper.rb中需求的顺序

这是第一次:

require 'rspec/rails' 
require 'rspec/autorun' 

require 'capybara/rspec' 
require 'capybara/rails' 
require "shoulda" 
require 'database_cleaner' 

# This file is copied to spec/ when you run 'rails generate rspec:install' 
ENV["RAILS_ENV"] ||= 'test' 
require File.expand_path("../../config/environment", __FILE__) 
为此:

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)

require 'rspec/rails'
require 'rspec/autorun'

require 'capybara/rspec' 
require 'capybara/rails' 
require "shoulda" 
require 'database_cleaner' 
因此,似乎必须首先加载
ENV
config/environment


谢谢大家让它运行起来。

这是一个很长的机会,但是您是否尝试过在
.html
文件中重新键入
edit\u user\u path
周围的所有字符,以防出现零宽度空格字符?另外,您是否打算在
.html
文件末尾包含
字符?还有,您的跑步鞋还有其他规格吗?您是否有任何视图中的路径在rspec中“起作用”?当你运行Rails时,这个视图能正常工作吗?嗯,没有零空格。。。另外,“index.html.erb”也不起作用。所有视图规范都失败。而且浏览器中的Rails应用程序本身可能工作得很好……好吧,所以它不是特定于视图的。您是否安装了
rspec-rails
gem?+1:-)并链接到,以防其他人遇到这种情况并发现其他问题。
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'

Spork.prefork do
  require 'rspec/rails'
  require 'rspec/autorun'
  require 'capybara/rspec'
  require 'capybara/rails'
  require "shoulda"
  require 'database_cleaner'

  # This file is copied to spec/ when you run 'rails generate rspec:install'
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)


  # Requires supporting ruby files with custom matchers and macros, etc,
  # in spec/support/ and its subdirectories.
  Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

  RSpec.configure do |config|
    # ## Mock Framework
    #
    # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
    #
    # config.mock_with :mocha
    # config.mock_with :flexmock
    # config.mock_with :rr
    config.mock_with :rspec

    config.include JsonSpec::Helpers

    # 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 = false

    # 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

    # Run specs in random order to surface order dependencies. If you find an
    # order dependency and want to debug it, you can fix the order by providing
    # the seed, which is printed after each run.
    #     --seed 1234
    config.order = "random"

    DatabaseCleaner.strategy = :truncation
    DatabaseCleaner.orm = "active_record"

    config.before(:suite) do
      DatabaseCleaner.clean
      DatabaseCleaner.strategy = :transaction
      DatabaseCleaner.clean_with(:truncation)
    end

    config.before(:each) do
      DatabaseCleaner.start
    end

    config.after(:each) do
      DatabaseCleaner.clean
    end
  end
end

Spork.each_run do
  # This code will be run each time you run your specs.
  # Fabrication.clear_definitions
  # Dir[Rails.root.join("spec/fabricators/**/*.rb")].each{|f| load f}
  DatabaseCleaner.clean

  Fabrication.clear_definitions

  Dir["#{Rails.root}/app/models/**/*.rb", "#{Rails.root}/app/services/**/*.rb", "#{Rails.root}/app/admin/**/*.rb","#{Rails.root}/app/mailers/**/*.rb"].each do |model|
    load model
  end
end
require 'rspec/rails' 
require 'rspec/autorun' 

require 'capybara/rspec' 
require 'capybara/rails' 
require "shoulda" 
require 'database_cleaner' 

# This file is copied to spec/ when you run 'rails generate rspec:install' 
ENV["RAILS_ENV"] ||= 'test' 
require File.expand_path("../../config/environment", __FILE__) 
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)

require 'rspec/rails'
require 'rspec/autorun'

require 'capybara/rspec' 
require 'capybara/rails' 
require "shoulda" 
require 'database_cleaner'