2014-09-25 33 views
1

我一直在嘗試,失敗,重構使用頁面對象rspec的一些特點,但我有一個很難用它。如何創建頁面對象類Rspec的特點

我不是一個有經驗的開發回報率。可能這是一個基本的錯誤。

我有一個規範的特點是這樣的:

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 

但是,我想通過一個登錄頁面對象這樣的分享我的另一特點sign_in方法:

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類。這是對的嗎?我應該把這個班放在其他路上嗎?

有人可以幫我處理呢? 非常感謝。

+1

你需要這個類在您的測試?或者你的代碼有這樣的東西是'spec_helper.rb'? :'Dir [Rails.root.join(「spec/support/**/*。rb」)]。each {| f |需要F}' – lcguida 2014-09-25 14:36:45

+0

你好rockskull ..不,我沒有在我的spec_helper代碼塊。現在它已經修復了。非常感謝... – 2014-09-25 14:45:34

+1

Gald幫助。我已經把這個答案。 – lcguida 2014-09-25 14:48:44

回答

2

你必須需要您的支持文件夾中的文件在你spec_helper.rb文件

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