【问题标题】:How to create Page Objects classes for Rspec Features如何为 Rspec 功能创建页面对象类
【发布时间】:2014-09-25 14:35:00
【问题描述】:

我一直在尝试使用页面对象重构一些 rspec 功能,但没有成功,但我遇到了困难。

我不是经验丰富的 RoR 开发人员。可能这是一个基本错误。

我有这样的规范功能:

require 'capybara/rspec'
feature 'Acessing the dashboard' do

    scenario 'User see the dashboard after login' do
      sign_in 'teste','123' 
      expect(page).to have_css 'h1', text: 'Dashboard'
    end

    def sign_in(login, password)
      visit '/'
      fill_in 'login', :with => login
      fill_in 'password', :with => password
      click_button 'Autenticar'
    end 
end

但是,我想通过这样的登录页面对象与其他功能共享我的登录方法:

class LoginPage
   include Capybara::DSL

   def sign_in(login, password)
      visit '/'
      fill_in 'login', :with => login
      fill_in 'password', :with => password
      click_button 'Authenticate'
   end
end

所以,我改变了我的功能:

feature 'Acessing the dashboard' do

    let(:login_page) { LoginPage.new }

    scenario 'User see the dashboard after login' do
      login_page.sign_in 'teste','123'
      expect(page).to have_css 'h1', text: 'Dashboard'
    end

end

但我收到以下错误:

 Failure/Error: let(:login_page) { LoginPage.new }
 NameError: uninitialized constant LoginPage

我真的不知道该怎么办。 我在 /spec/support/LoginPage.rb 中创建了 LoginPage 类。这是正确的吗?我应该把这个类放在其他路径吗?

有人可以帮我处理吗? 非常感谢。

【问题讨论】:

  • 你在测试中需要这个类吗?或者你的代码有这样的东西是spec_helper.rb? :Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
  • 你好 rockkull.. 不,我的 spec_helper 中没有代码块。现在它已经修复了。非常感谢...
  • 高德来帮忙。我已经把它放在一个答案中。

标签: ruby-on-rails ruby rspec


【解决方案1】:

您必须在spec_helper.rb 文件中要求您的支持文件夹文件

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

【讨论】:

    猜你喜欢
    • 2016-11-11
    • 1970-01-01
    • 2010-12-30
    • 1970-01-01
    • 2015-05-20
    • 2021-06-25
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    相关资源
    最近更新 更多