为什么我的验收测试会出现错误?

Why do I get the error in my acceptance test?

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

看不懂,如果我写:

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>

我认为每个测试的环境都是重新设置的。新session、cookies等,很多情况下,甚至会产生全新的用户。所以一个 "global" 登录是不可能的。

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

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