这是我的要求规范:
require 'spec_helper'
describe "Password pages" do
describe "user views his passwords" do
let(:user) { create(:user) }
sign_in(:user)
end
end
spec_helper.rb
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.include Capybara::DSL
config.include LoginMacros
end
支持/ login_macros.rb
module LoginMacros
def sign_in(user)
visit new_user_session_path
fill_in 'Email', with: user.email
fill_in 'Password', with: user.password
click_button 'Sign in'
end
end
运行规范时出现错误:
/spec/requests/account_pages_spec.rb:9:in `block (2 levels) in <top (required)>': undefined method `sign_in' for #<Class:0x007fd4f97183f8> (NoMethodError)
答案 0 :(得分:0)
我使用这种很酷的技术,但另一种方式:
创建页面类:
class PageObject
include Capybara::DSL
def visit_page(page)
visit(page)
self
end
def login(user)
fill_in 'Email', with: user.email
fill_in 'Password', with: user.password
click_button 'Sign in'
end
end
创建feature
而不是describe
:
require 'spec_helper'
feature 'test authozire page' do
let(:login_page) { PageObject.new }
let(:user) { create(:user) }
scenario 'login page have field' do
page = login_page.visit_page(new_user_session_path)
expect(page.have_field?('email')).to be_true
expect(page.have_field?('password')).to be_true
end
scenario "user can login" do
login_page.visit_page(new_user_session_path).login(user)
# more awesome test
end
end