2014-06-08 5 views
5

Ich versuche, den $ rspec-Befehl auszuführen, aber alle meine Controller-Tests schlagen fehl, wenn ich explizit :type => :controller zu jeder der Spezifikationen hinzufüge.RSpec-Controller-Spezifikationen funktionieren nur, wenn explizit Folgendes hinzugefügt wird: type =>: controller

Hier ist der Fehler Ich erhalte:

1) AccountsController GET index assigns all accounts as @accounts 
    Failure/Error: get :index, {}, valid_session 
    NoMethodError: 
     undefined method `get' for #<RSpec::ExampleGroups::AccountsController_2::GETIndex:0x007fd96c8a6a68> 
    # ./spec/controllers/accounts_controller_spec.rb:36:in `block (3 levels) in <top (required)>' 

Hier ist die erzeugte spec:

require 'spec_helper' 

# This spec was generated by rspec-rails when you ran the scaffold generator. 
# It demonstrates how one might use RSpec to specify the controller code that 
# was generated by Rails when you ran the scaffold generator. 
# 
# It assumes that the implementation code is generated by the rails scaffold 
# generator. If you are using any extension libraries to generate different 
# controller code, this generated spec may or may not pass. 
# 
# It only uses APIs available in rails and/or rspec-rails. There are a number 
# of tools you can use to make these specs even more expressive, but we're 
# sticking to rails and rspec-rails APIs to keep things simple and stable. 
# 
# Compared to earlier versions of this generator, there is very limited use of 
# stubs and message expectations in this spec. Stubs are only used when there 
# is no simpler way to get a handle on the object needed for the example. 
# Message expectations are only used when there is no simpler way to specify 
# that an instance is receiving a specific message. 

describe AccountsController do 

    # This should return the minimal set of attributes required to create a valid 
    # Account. As you add validations to Account, be sure to 
    # adjust the attributes here as well. 
    let(:valid_attributes) { { "subdomain" => "MyString" } } 

    # This should return the minimal set of values that should be in the session 
    # in order to pass any filters (e.g. authentication) defined in 
    # AccountsController. Be sure to keep this updated too. 
    let(:valid_session) { {} } 

    describe "GET index" do 
    it "assigns all accounts as @accounts" do 
     account = Account.create! valid_attributes 
     get :index, {}, valid_session 
     assigns(:accounts).should eq([account]) 
    end 
    end 
... 

Wenn ich in der spec/rails_helper.rb Datei anschaue, sehe ich diese Schnipsel:

# RSpec Rails can automatically mix in different behaviours to your tests 
    # based on their file location, for example enabling you to call `get` and 
    # `post` in specs under `spec/controllers`. 
    # 
    # You can disable this behaviour by removing the line below, and instead 
    # explicitly tag your specs with their type, e.g.: 
    # 
    #  RSpec.describe UsersController, :type => :controller do 
    #  # ... 
    #  end 
    # 
    # The different available types are documented in the features, such as in 
    # https://relishapp.com/rspec/rspec-rails/docs 
    config.infer_spec_type_from_file_location! 

Aber ich bin neu zu RSpec und TDD und ich bin mir nicht sicher, ob diese rails_helper.rb Datei gelesen wird oder nicht.

spec/spec_helper.rb

# 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 'email_spec' 
require 'rspec/autorun' 

# 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| 
    config.include(EmailSpec::Helpers) 
    config.include(EmailSpec::Matchers) 
    # ## 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 

    # 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 

    # 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" 

    config.before(:suite) do 
    DatabaseCleaner.strategy = :truncation 
    end 
    config.before(:each) do 
    DatabaseCleaner.start 
    end 
    config.after(:each) do 
    DatabaseCleaner.clean 
    end 
end 

Kann mir jemand sagen, wie ich meine Controller-Spezifikationen, ohne explizit zu laufen bekommen kann :type => :controller hinzufügen? Die Controller-Spezifikationen sind alle im Verzeichnis spec/controllers, so dass RSpec nur wissen sollte, dass sie Controller-Spezifikationen sind.

Antwort

9

Du läufst RSpec 3 und Sie haben nicht vollständig aktualisiert Ihre Konfiguration von RSpec 2.

rspec-Schienen 3 'spec_helper.rb' und 'rails_helper.rb' erzeugt. 'rails_helper.rb' benötigt 'spec_helper.rb'. In rspec-rails 3 sollten die Spezifikationen der Rails-Klassen require 'rails_helper', nicht require 'spec_helper' lauten, da die generierte Spezifikation dies tut. Also, entweder

  • Änderung require 'spec_helper'-require 'rails_helper',
  • regenerieren Ihr Gerüst (ich glaube, Ihre spec wurde von rspec-Schienen erzeugt 2) oder
  • entfernen die insgesamt benötigt und setzte --require rails_helper in Ihrem Projekt .rspec Datei Es ist automatisch in allen Spezifikationen enthalten.

Und dann wird config.infer_spec_type_from_file_location! wirksam werden.

+0

Das scheint mich über diesen bestimmten Fehler zu bringen. Vielen Dank. – Catfish

Verwandte Themen