4

我遇到了奇怪的问题。我以前从未使用过 Devise gem。所以,我尝试编写简单的 RSpec 测试:

require 'spec_helper'

describe "Admins" do
  before(:each) do
    @email = "test@test.ru"
    user = FactoryGirl.create(:user, :email => @email )
  end

  describe "sign in/out" do
    describe "success" do
      it "should sign a user in and out" do
        visit new_user_session_path
      end
    end
  end
end

我收到这样的错误:

  1) Admins sign in/out success should sign a user in and out
     Failure/Error: visit new_user_session_path
     NoMethodError:
       undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0x007fb2b7e393d0>
     # ./spec/requests/admins_spec.rb:12:in `block (4 levels) in <top (required)>'

Finished in 0.05646 seconds

我的工厂:

FactoryGirl.define do
  factory :user do |user|
    user.admin true
    user.password "qwerty"
    user.sequence(:email) { |n| "user#{n}@user.com" }
  end
end

路线:

        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
4

3 回答 3

19

将任何使用 capybara 的测试从规范/请求移动到规范/功能。

http://alindeman.github.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html

于 2012-11-21T22:45:34.610 回答
4

visit是水豚方法,而不是Rspec。就像 ArcaneRain 指出的那样,您必须将capybaragem 添加到您的 Gemfile、run和bundlespec_helper中。require 'capybara/rspec'

于 2012-06-14T13:12:12.683 回答
2

您必须将 Capybara DSL 添加到 spec_helper

.
.
.
RSpec.configure do |config|
  .
  .
  .
  config.include Capybara::DSL
end
于 2013-09-05T17:48:38.697 回答