【问题标题】:error using rspec for devise使用 rspec 进行设计时出错
【发布时间】:2012-07-02 13:35:53
【问题描述】:

我正在使用 rspec 来测试设备身份验证。以下是我的代码

require 'spec_helper'

describe User do

describe "user registration" do
it "allows new users to register with an email address and password" do
get "/users/sign_up"

fill_in "Email",                 :with => "abc@example.com"
fill_in "Password",              :with => "abc123"
fill_in "Password confirmation", :with => "abc123"

 click_button "Sign up"

   response.should have_content("Welcome! You have signed up successfully.")
  end
 end
end

我收到以下错误。

"NoMethodError:undefined method `get' for #"

【问题讨论】:

    标签: ruby-on-rails rspec devise


    【解决方案1】:

    您正在模型规范中使用控制器方法和集成测试方法 (Capybara)。它不会起作用。

    模型规范(UNIT 测试)将包含以下内容:

    1. 测试您的验证者/关系
    2. 测试范围
    3. 模型的方法

    查看有关使用 RSpec 进行测试的这一系列博客文章,它应该会有所帮助: http://everydayrails.com/2012/03/12/testing-series-intro.html

    【讨论】:

      【解决方案2】:

      这似乎是一个模型规范 (describe User),它不允许运行请求,但您可能想要编写一个控制器规范 (describe UsersController) 甚至是集成测试。

      如果您使用默认的 rspec 布局,只需将您的代码移动到适当的目录(spec/controllersspec/integration)。我会做一个集成测试:

      # In spec/integration/user_registration_spec.rb
      require 'spec_helper'
      
      describe "User registration" do
        it "allows new users to register with an email address and password" do
          get "/users/sign_up"
      
          fill_in "Email",                 :with => "abc@example.com"
          fill_in "Password",              :with => "abc123"
          fill_in "Password confirmation", :with => "abc123"
      
          click_button "Sign up"
      
          response.body.should have_content("Welcome! You have signed up successfully.")
        end
      end
      

      【讨论】:

        【解决方案3】:

        这个文件在 spec/models 目录中吗?我猜是这种情况,因为您是describe@User。您编写测试的方式是控制器式测试和集成(验收)测试的混合。这可能就是你想要的:

        require 'spec_helper'
        
        describe User do
          describe "user registration" do
            it "allows new users to register with an email address and password" do
              visit "/users/sign_up"
        
              fill_in "Email",                 :with => "abc@example.com"
              fill_in "Password",              :with => "abc123"
              fill_in "Password confirmation", :with => "abc123"
        
              click_button "Sign up"
        
              page.should have_content("Welcome! You have signed up successfully.")
            end
          end
        end
        

        将此文件放在 spec/integration 或 spec/requests 目录中。

        【讨论】:

        • 我的应用程序中没有 spec/integration 或 spec/requests 目录。我想测试设计认证。由于设计只在应用程序中自己生成模型,所以没有用户控制器,这就是我在模型测试中编写此代码的原因。
        • 只需创建目录 spec/requests 并将此文件移动到其中。
        • 你还没有迁移数据库,那么
        • 根据您的建议,我在创建目录后移动了文件,现在出现 Failure/Error: get "/users/sign_up" ActiveRecord::StatementInvalid: Could not find table 'users' – shah khan 7 分钟前
        • 您是否还运行了rake db:test:prepare 以将架构放入测试数据库?
        【解决方案4】:

        我可能会尝试这样的事情

        require 'spec_helper'
        
        describe User do
        
        describe "user registration" do
         it "allows new users to register with an email address and password" do
          visit new_user_registration_path
          current_path.should be(new_user_registration_path)
        
          fill_in "user[email]",                 :with => "abc@example.com"
          fill_in "user[password]",              :with => "abc123"
          fill_in "user[password_confirmation]", :with => "abc123"
          click_button "Sign up"
        
          expect { click_button submit }.to change(User, :count).by(1)
          response.should be_redirect   
          response.should have_content("Welcome! You have signed up successfully.")
          end
         end
        end
        

        但我强烈建议使用FactoryGirl 生成新值。还要检查您使用了哪些设计模块。例如,如果您使用的是 Confirmable 模块,很明显这种方法是错误的。一些有用的article

        【讨论】:

          猜你喜欢
          • 2013-12-02
          • 1970-01-01
          • 2011-12-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-10
          • 1970-01-01
          相关资源
          最近更新 更多