0

我正在使用 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:未定义的方法 `get' for #”

4

4 回答 4

2

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

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

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

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

于 2012-07-02T13:42:20.023 回答
0

我可能会尝试这样的事情

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 模块,很明显这种方法是错误的。一些有用的文章

于 2012-07-03T06:52:45.213 回答
0

这似乎是一个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
于 2012-07-02T13:42:09.370 回答
0

这个文件在 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 目录中。

于 2012-07-02T13:46:15.607 回答