重構 sign_in_spec

require "rails_helper"

describe "user sign in", :type => :feature do
  before :each do
    User.create(:email => "[email protected]", :password => "password")
  end

  it "sign_in the user" do
    new_session_page.sign_in "[email protected]", "password"

    expect(page).to have_content "[email protected]"
  end
end

顯示錯誤訊息:

F

Failures:

  1) user sign in sign_in the user
     Failure/Error: new_session_page.sign_in '[email protected]', 'password'

     NameError:
       undefined local variable or method `new_session_page' for #<RSpec::ExampleGroups::UserSignIn:0x007fd55c0c8e68>
     # ./spec/features/user_sign_in_spec.rb:12:in `block (2 levels) in <top (required)>'

Finished in 0.0321 seconds (files took 1.95 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/features/user_sign_in_spec.rb:10 # user sign in sign_in the user

新增

+ private
+
+ def new_session_page
+   home_page.go
+   navbar.sign_in
+ end

顯示錯誤訊息:

F

Failures:

  1) user sign in sign_in the user
     Failure/Error: home_page.go

     NameError:
       undefined local variable or method `home_page' for #<RSpec::ExampleGroups::UserSignIn:0x007fbca21543a0>
     # ./spec/features/user_sign_in_spec.rb:17:in `new_session_page'
     # ./spec/features/user_sign_in_spec.rb:12:in `block (2 levels) in <top (required)>'

Finished in 0.02566 seconds (files took 1.49 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/features/user_sign_in_spec.rb:10 # user sign in sign_in the user
private

def home_page
  PageObjects::Pages::Home.new
end

+ def new_session_page
+   home_page.go
+   navbar.sign_in
+ end

顯示錯誤訊息:

F

Failures:

  1) user sign in sign_in the user
     Failure/Error: navbar.sign_in

     NameError:
       undefined local variable or method `navbar' for #<RSpec::ExampleGroups::UserSignIn:0x007fa929e512b0>
     # ./spec/features/user_sign_in_spec.rb:24:in `new_session_page'
     # ./spec/features/user_sign_in_spec.rb:12:in `block (2 levels) in <top (required)>'

Finished in 0.2593 seconds (files took 1.46 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/features/user_sign_in_spec.rb:10 # user sign in sign_in the user
def new_session_page
  home_page.go
  navbar.sign_in
end

+ def navbar
+   PageObjects::Application::Navbar.new
+ end

顯示錯誤訊息:

F

Failures:

  1) user sign in sign_in the user
     Failure/Error: PageObjects::Application::Navbar.new

     NameError:
       uninitialized constant PageObjects::Application
     # ./spec/features/user_sign_in_spec.rb:24:in `navbar'
     # ./spec/features/user_sign_in_spec.rb:29:in `new_session_page'
     # ./spec/features/user_sign_in_spec.rb:12:in `block (2 levels) in <top (required)>'

Finished in 0.24551 seconds (files took 1.39 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/features/user_sign_in_spec.rb:10 # user sign in sign_in the user

新增 spec/support/page_objects/application/navbar.rb

# Comment Edit
require_relative "../base"

module PageObjects
  module Application
    class Navbar < Base
      def sign_in
        click_on "Login"
        PageObjects::Devise::Sessions::New.new
      end
    end
  end
end

新增 spec/support/page_objects/devise/sessions/new.rb

require_relative "../../base"

module PageObjects
  module Devise
    module Sessions
      class New < Base

        def sign_in(email, password)
          within(".new_user") do
            fill_in 'Email', :with => '[email protected]'
            fill_in 'Password', :with => 'password'
          end
          click_button "Log in"
        end
      end
    end
  end
end

results matching ""

    No results matching ""