2016-09-08 70 views
0

请告诉我,为什么会发生?为什么我的验收测试中出现错误?

我不明白,如果我写:

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 => '[email protected]' 
    fill_in :user_username, :with => 'mike' 
    fill_in :user_password, :with => '[email protected]#' 
    fill_in :user_password_confirmation, :with => '[email protected]#' 

    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> 

回答

0

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

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

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

+0

我该怎么做,它会正确吗? – Vladimir

+0

做'之前:每个'。 –

相关问题