简单的脚手架放:ActionView :: Template ::错误:未定义的方法`edit_user_path'

时间:2013-09-24 14:03:56

标签: 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>" 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

用户/ show.html.erb

<p id="notice"><%= notice %></p>

<p>
  <b>First name:</b>
  <%= @user.first_name %>
</p>

<p>
  <b>Last name:</b>
  <%= @user.last_name %>
</p>

<%= link_to 'Edit', edit_user_path(@user) %> |

的routes.rb

resources :users

佣金路线表示用户存在路线

               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 '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

1 个答案:

答案 0 :(得分:3)

与彼得聊了一个小时。问题是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' 

因此,似乎必须首先加载ENVconfig/environment

谢谢大家的帮助。