【问题标题】:Why do I get the error in my acceptance test?为什么我在验收测试中出现错误?
【发布时间】:2016-09-08 10:14:33
【问题描述】:

请告诉我,为什么会这样?

我不明白,如果我写的话:

feature "Article Creation" do

#here i write (:all)

before(:all) do
  sign_up_helper
end

我得到错误:

 Article Creation allows user to visit to creating article page
 Failure/Error: fill_in :article_title, :with => 'test_title'

 Capybara::ElementNotFound:
   Unable to find field :article_title

1) Article Creation allows user to visit to article page
 Failure/Error: expect(page).to have_content I18n.t('articles.articles_new')
   expected to find text "New Article:" in "Toggle navigation Blog Rails New Contacts Sign in --- !ruby/hash:ActionController::Parameters controller: devise/sessions action: new {\"controller\"=>\"devise/sessions\", \"action\"=>\"new\"} nil You need to sign in or sign up before continuing. Sign in: Email Password Remember me Sign up Forgot your password?"

但是,如果我写:

feature "Article Creation" do

 #here i write(:each)

 before(:each) do
   sign_up_helper
 end

没关系。所有测试都有效。我的问题 -为什么?

这是我的测试

*#before all test visitor signs up
#here I've changed :all and :each*
feature "Article Creation" do
before(:all) do
  sign_up_helper
end

scenario "allows user to visit to article page" do
  visit new_article_path

  expect(page).to have_content I18n.t('articles.articles_new')
end

scenario "allows user to visit to created article page" do
  visit new_article_path


  fill_in :article_title, :with => 'test_title'
  fill_in :article_text, :with => 'example_text'
  click_button 'Save Article'

  expect(page).to have_content 'example_text'
end

这是 sign_up_helper 方法

#spec/support/session_helper.rb
def sign_up_helper
  visit new_user_registration_path

  fill_in :user_email, :with => 'user@example.com'
  fill_in :user_username, :with => 'mike'
  fill_in :user_password, :with => 'secure123!@#'
  fill_in :user_password_confirmation, :with => 'secure123!@#'

    click_button 'Sign up'
end

这是html表单:

<p>
  <label for="article_title">Title</label><br/>
  <input type="text" name="article[title]" id="article_title" />
  </p>

<p>
  <label for="article_text">Text</label><br/>
  <textarea name="article[text]" id="article_text">
  </textarea>
</p>

【问题讨论】:

    标签: ruby-on-rails rspec capybara acceptance-testing


    【解决方案1】:

    我认为每次测试的环境都是重新设置的。新会话、cookie 等。在许多情况下,甚至会生成全新的用户。所以一个“全局”登录是不可能的。

    即使有可能,它仍然是一个问题,因为它引入了 规范顺序依赖性,这是很糟糕的。想象一下,您的一个规范将用户注销。然后每个后续规范都会失败,因为用户不再登录。

    为防止这种情况发生,请确保每个规范都根据需要设置自己的环境(用户登录、方法存根等),而不依赖于先前执行的规范的副作用(可能会或可能不会持续存在)。

    【讨论】:

    • before :each.
    猜你喜欢
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多