【问题标题】:Duplicate Email with Capybara使用 Capybara 重复电子邮件
【发布时间】:2015-07-04 19:12:19
【问题描述】:

我是 ruby​​ on rails 的初学者。我正在尝试使用 rspec 和 capybara gems 测试重复的电子邮件场景。

这是我的用户测试规范:

    require 'rails_helper'

feature "User signs up", type: :feature do
  # before do
  #   click_link "Sign up"
  #   expect(current_path).to eq(new_user_registration_path)
  #   fill_in "Name", with: "Test"
  #   fill_in "Surname", with: "Test"
  #   fill_in "Email", with: "tester@example.tld"
  #   fill_in "Password", with: "test-password"
  #   fill_in "Password confirmation", with: "test-password"
  #   click_button "Sign up"
  # end

  scenario "successfully" do
    visit "/"

    click_link "Sign up"
    expect(current_path).to eq(new_user_registration_path)
    fill_in "Name", with: "Test"
    fill_in "Surname", with: "Test"
    fill_in "Email", with: "tester@example.tld"
    fill_in "Password", with: "test-password"
    fill_in "Password confirmation", with: "test-password"
    click_button "Sign up"

    expect(current_path).to eq "/"
    expect(page).to have_content(
      "A message with a confirmation link has been sent to your email address.
      Please follow the link to activate your account."
    )

    open_email "tester@example.tld", with_subject: "Confirmation instructions"
    visit_in_email "Confirm my account"

    expect(current_path).to eq new_user_session_path
    expect(page).to have_content "Your email address has been successfully confirmed."

    fill_in "Email", with: "tester@example.tld"
    fill_in "Password", with: "test-password"
    click_button "Log in"

    expect(current_path).to eq "/"
    expect(page).to have_content "Signed in successfully."
    expect(page).to have_content "Hello Test"


  end

  scenario "usuccessfully - Invalid email " do
    visit "/"

    click_link  "Sign up"
    expect(current_path).to eq(new_user_registration_path)
    fill_in "Name", with: "Test"
    fill_in "Surname", with: "Test"
    fill_in "Email", with: "invalid-email"
    fill_in "Password", with: "test-password"
    fill_in "Password confirmation", with: "test-password"
    click_button "Sign up"
    expect(current_path).to eq "/users"
    expect(page).to have_content(
      "Email is invalid"
        )
  end
  scenario "usuccessfully - Duplicate email " do
    visit "/"

    click_link "Sign up"
    expect(current_path).to eq(new_user_registration_path)
    fill_in "Name", with: "Test"
    fill_in "Surname", with: "Test"
    fill_in "Email", with: "tester@example.tld"
    fill_in "Password", with: "test-password"
    fill_in "Password confirmation", with: "test-password"
    click_button "Sign up"
    expect(current_path).to eq "/users"
    expect(page).to have_content(
      "Email has already been taken"
        )
  end

end

feature "User signing", type: :feature do
  scenario "usuccessfully" do
    visit "/"

    click_link "Sign in"
    expect(current_path).to eq(new_user_session_path)
    fill_in "Email", with: "wrong-email@example.tld"
    fill_in "Password", with: "wrong-password"
    click_button "Log in"

    expect(current_path).to eq(new_user_session_path)
    expect(page).to have_content(
      "Invalid email or password."
    )

    # expect(current_path).to eq new_user_session_path
    # expect(page).to have_content "Your email address has been successfully confirmed."

    # fill_in "Email", with: "tester@example.tld"
    # fill_in "Password", with: "test-password"
    # click_button "Log in"

    # expect(current_path).to eq "/"
    # expect(page).to have_content "Signed in successfully."
    # expect(page).to have_content "Hello Zein"


  end
end

我收到以下故障:

  1) User signs up usuccessfully - Duplicate email 
     Failure/Error: expect(current_path).to eq "/users"

       expected: "/users"
            got: "/"

       (compared using ==)
     # ./spec/features/user_sign_ups_spec.rb:77:in `block (2 levels) in <top (required)>'

如何使用 capybara gem 创建重复用户? 感谢您的帮助:)

【问题讨论】:

    标签: ruby-on-rails ruby rspec capybara


    【解决方案1】:

    看看Factory Girl gem(如果您还没有使用它)。让我们从一个简单的user 工厂开始:

    FactoryGirl.define do
      factory :user do
        sequence(:email) { |n| "person#{n}@example.com" }
        first_name 'Tyrion'
        last_name 'Lannister'
        password 'test1234'
      end
    end 
    

    然后您可以构建一个已经存在具有给定电子邮件的用户的测试 - 只需在测试本身之前执行的 before 块中创建一个:

    context 'email is already taken' do
      before do
        create(:user, email: 'taken@gmail.com')
      end
      it 'notifies the user about a duplicate email' do
        # visit your page
        fill_in 'Email', with: 'taken@gmail.com'
        # fill in the rest of the fields
        click_button 'Sign up'
        expect(page).to have_content 'Email has already been taken'
      end
    end
    

    【讨论】:

      【解决方案2】:

      你的方法很接近。你应该做的是创建一个User(你可以像其他人已经建议的那样使用FactoryBot,或者像User.create!这样在测试中创建一个),然后尝试使用相同的信息注册用户您的“填写”方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-01-18
        • 1970-01-01
        • 2013-02-14
        • 1970-01-01
        • 2012-03-22
        • 2015-12-10
        • 2017-06-29
        相关资源
        最近更新 更多