2

我有这个简单的脚手架:

规格:

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) %> |

路线.rb

resources :users

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

1 回答 1

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' 

因此,似乎必须先加载ENVand 。config/environment

谢谢大家让这个运行。

于 2013-09-24T17:05:29.367 回答