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

时间:2016-09-08 10:14:33

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

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

如果我写的话,我无法理解:

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>

1 个答案:

答案 0 :(得分:0)

我认为每次测试的环境都是重新设定的。新会话,cookie等。在许多情况下,甚至会生成全新的用户。所以一个&#34;全球&#34;无法登录。

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

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