我有一個spec/controllers/add_to_carts_spec.rb
:未定義的方法`sign_in' <:: RSpec的核心:: ExampleGroup :: Nested_1 :: Nested_1:0x1057fd428>錯誤,而試圖建立與RSpec的設計
require 'spec_helper'
describe CartItemsController do
before (:each) do
@user = Factory(:user)
sign_in @user
end
describe "add stuff to the cart" do
it "should add a product to the cart" do
product = FactoryGirl.create(:product)
visit products_path(product)
save_and_open_page
click_on('cart_item_submit')
end
end
end
和/spec/support/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 'capybara/rspec'
# 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.mock_with :rspec
config.use_transactional_fixtures = true
end
...同時也裝入/spec/support/devise.rb
:
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
衛隊在後臺運行,並不斷拋出這樣的:
Failures:
1) CartItemsController add stuff to the cart should add a product to the cart
Failure/Error: sign_in @user
NoMethodError:
undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x1057fd428>
# ./spec/controllers/add_to_carts_spec.rb:7
我已經花了幾個小時嘗試不同的配置調整和不同的語法,但似乎沒有任何改變。有任何想法嗎?
(編輯,以反映新的錯誤)
這是一個控制器規格,所以我期望它的工作。 – solnic
謝謝,你是對的。我混淆了兩種規格。我正在使用水豚訪問頁面,因此使用其會話機制,但嘗試登錄用戶,就像我在我的控制器規格中那樣。水豚不使用或訪問這些。 –