7

私はキュウリを初めて使用し、Deviseログイン機能をテストするための次のスニペットを見つけました。ただし、もう1つのステップが欠落しているようで、解決策が見つかりませんでした。

Given /^that a confirmed user exists$/ do
  pending # express the regexp above with the code you wish you had
end

ここに次のコードがあります:

features / authentication / session.feature

Feature: Session handling
  In order to use the site
  As a registered user
  I need to be able to login and logout

Background: 
  Given that a confirmed user exists

Scenario Outline: Logging in
  Given I am on the login page
  When I fill in "user_email" with "<email>"
  And I fill in "user_password" with "<password>"
  And I press "Sign in"
  Then I should <action>
  Examples:
    |         email       |  password   |              action             |
    | minimal@example.com |  test1234   | see "Signed in successfully"    |
    | bad@example.com     |  password   | see "Invalid email or password" |

Scenario: Logging out
  Given I am logged in
  When I go to the sign out link
  Then I should see "Signed out successfully"

features / step_definitions / authentication_steps.rb

# Session
Given /^I am logged in$/ do
  visit path_to('the login page')
  fill_in('user_email', :with => @user.email)
  fill_in('user_password', :with => @user.password)
  click_button('Sign in')
  if defined?(Spec::Rails::Matchers)
    page.should have_content('Signed in successfully')
  else
    assert page.has_content?('Signed in successfully')
  end
end

spec / factorys / user.rb

Factory.define :minimal_user, :class => User do |u|
  u.username 'minimal'
  u.email 'minimal@example.com'
  u.password 'test1234'
  u.password_confirmation 'test1234'
end

ここに元のコードへのリンクがあります

助けてくれて本当にありがとうございます!!

4

3 に答える 3

3

タイトルには「ユーザーが存在することを確認する」と書かれていますが、それはあなたがそこで行う必要があることではないかもしれません-あなたのGivenステップは何かが機能したことを主張する必要はありません、彼らはあなたのシナリオのアプリケーション状態を作成するためにコードを呼び出します。もちろん、まだ失敗する可能性があるため、まだテスト中です。

私はあなたがこのようなものを探していると思います:

Given /^that a confirmed user exists$/ do
  Factory.create(:minimal_user)
end

これにより、ファクトリ定義から新しい確認済みユーザーが作成および保存されるため、シナリオの残りの部分を続行できます。

于 2010-11-02T23:38:02.080 に答える
1

ダニエルの答えを完成させるために、そして私はDevise確認可能モジュールを有効にしているので、ユーザーが確認されたことを伝えるためにフィクスチャに行を追加する必要があります。

例えば:

Factory.define :minimal_user, :class => User do |u|
  u.username 'minimal'
  u.email 'minimal@example.com'
  u.password 'test1234'
  u.password_confirmation 'test1234'
  u.confirmed_at 'here the date you want'
end

また、ここにあるデバッグ手順は非常に便利です。

それが何人かの人々に役立つことを願っています。

于 2010-11-03T14:19:39.747 に答える
0
(::) failed steps (::)

expected #has_content?("Signed in successfully") to return true, got false (RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/web_steps.rb:110:in `block (2 levels) in <top (required)>'
./features/step_definitions/web_steps.rb:14:in `with_scope'
./features/step_definitions/web_steps.rb:108:in `/^(?:|I )should see "([^"]*)"(?: within "([^"]*)")?$/'
features/authentication/session.feature:16:in `Then I should <action>'

undefined method `email' for nil:NilClass (NoMethodError)
./features/step_definitions/authentication_steps.rb:43:in `/^I am logged in$/'
features/authentication/session.feature:23:in `Given I am logged in'
于 2010-11-03T00:06:52.667 に答える