DRY与重复功能规范

时间:2014-04-25 15:53:45

标签: ruby rspec

我创建了一个测试用户如何确认其帐户的规范。它在以下场景中测试用户:

  • 可以注册
  • 如果他提供了正确的密钥,可以确认他的帐户
  • 如果未登录
  • ,则无法确认其帐户 如果他的密钥无效,
  • 无法确认他的帐户
  • 如果他的钥匙属于其他人,
  • 无法确认他的帐户。

虽然下面的代码完全没有经过测试(在浏览器中全部写完)但我不明白为什么它不起作用。但是,这些是我的担忧:

  • 很难理解。我只是在这里测试几个场景,还有几十个
  • 缩进将因此而变得非常疯狂(5或7个嵌套场景)
  • 那我该如何解决这个问题呢?功能规格实际上应该不那么干吗?
  • 功能规格是否应该有些重复?我应该取消嵌套场景吗?如果每个场景使用自己的场景与每个场景处于相同的“级别”,那么在块之前自包含偶尔会有与其他块之前相同的代码吗?这样做会更少干,但更易读,更容易适应。

同样,这段代码甚至从未见过崇高文字的光芒,所以可能会有一些小问题:

DRYer但更难遵循:

describe 'user spec', js: true do
    before { visit root_path }
    let(:user){ FactoryGirl.build(:user) }       

    module Helpers
        def deliveries
            ActionMailer::Base.deliveries
        end

        def last_email
           deliveries.last
        end

        def sent_emails_count
           deliveries.count
        end

        def reset_action_mailer
           deliveries = []
        end

        def reload_user(user_name)

            User.find_by(user_name: user_name) # like to know how to write a method that could find by any attribute, not just username. E.g. `reload_user(email: 'thisismyemail@gotmail.com)`
        end

        def register_user(user)
           click_link 'sign up today!'
           fill_in 'user_user_name', with: user.user_name
           fill_in 'user_email', with: user.email
           fill_in 'user_password', with: user.password
           fill_in 'user_password_confirmation', with: user.password_confirmation
           click_button 'Register'
        end

        def sign_out(user) # I have a 'useless' user parameter in there to make code more self-documenting. E.g. sign_out(user_1), sign_out(user_2)
           click_link 'sign out'
        end

       def sign_in(user) 
           click_link 'sign_in'
           fill_in 'user_email', with: user.user_name
           fill_in 'user_password', with: user.password
        end
    end

# shared examples

   shared_examples_for 'a succeeded confirmation attempt' do |user|
       describe 'the page' do
           subject { page }

           it_should_behave_like 'profile_page'
           it{ should have_selector '.alert-success', text: "Thankyou for confirming! You're now a fully fledged user!" }
           it{ should have_selector 'h4', text: 'confirmed' }
       end

       describe 'the instance' do
           it_should_behave_like 'an confirmed instance', user
       end
   end    

   shared_examples_for 'a failed confirmation_attempt' do |user|
      describe 'the page' do
           subject { page }

           it_should_behave_like 'profile_page'

           it{ should have_selector '.alert-error', text: "Sorry, that key is wrong!" }
           it{ should have_selector 'h4', text: 'unconfirmed' }
       end



       describe 'the instance' do
           it_should_behave_like 'an unconfirmed instance', user
       end
    end

    shared_examples_for 'an unconfirmed instance' do |user|
        subject { reload_user(user.user_name) } 

        its(:confirmed_at){ should be_nil }         
        its(:state){ should eq 'unconfirmed' }
    end

    shared_examples_for 'a confirmed instance' do |user|
        subject { reload_user(user.user_name) } 

        its(:confirmed_at){ should eq Time.now }         
        its(:state){ should eq 'confirmed' }
    end


    scenario 'user signs up' do
       before do
           register_user(user)
        end

       describe 'the page' do
           subject { page }

           it_should_behave_like 'profile_page' # shared example that tests content and title. 
           it{ should have_selector '.alert-success', text: "welcome to the site, #{user.user_name}! Please check your inbox for a confirmation email. You won't be able to do anything untill you confirm your account!" }
           it{ should have_selector 'h4', text: user.email }
           it{ should have_selector 'h4', text: user.user_name }
           it{ should have_selector 'h4', text: 'unconfirmed' }

       end   


       describe 'the instance' do
           subject { User.last } #is this the best way to do this? I want to do `user.reload` but I can't reload user because the user is built in memory (doesn't go through ActiveRecord), and so doesn't have an id

           its(:confirmation_key_created_at){ should eq Time.now } 
           its(:confirmation_key){ should be_base64 } # my own matcher. No, I'm not an autistic maths genius, just testing it's 64 characters long ;)
           its(:state){ should eq 'unconfirmed' }
       end

       describe 'last email' do
          subject { last_email }
          # using email-spec matchers https://github.com/bmabey/email-spec

          it { should have_subject "Hi #{user.user_name}, confirmation link enclosed!" }              
          it { should deliver_to user.email }
          it { should have_body_text user.confirmation_key }

       end

       scenario 'then attempts confirmation by following the confirmation link in his email' do
           before do
              visit culminate_path(user.confirmation_key) # In order to closer emulate what the user would do, I would like to actually get the last email and use capybara to click the confirmation link, rather then emulating the click as I'm doing here. How could I do this?
           end

           it_should_behave_like 'a succeeded confirmation attempt'


       end



       scenario 'then attempts confirmation by following a made up link' do
           let(:user_2) { FactoryGirl.build(:user) }

           before do
              sign_out(user)
              register_user(user_2) # could use FactoryGirl.create but this is an integration test. I want to emulate real-world events as much as possible 
              sign_out(user_2)
              sign_in(user)
              visit culminate_path(SecureRandom.urlsafe_base64) 
           end

           it_should_behave_like 'a failed confirmation_attempt'

       end 

       scenario 'then attempts confirmation by following a confirmation link for a different user' do
           let(:user_2) { FactoryGirl.build(:user) }

           before do
              sign_out(user)
              register_user(user_2) # could use FactoryGirl.create but this is an integration test. I want to emulate real-world events as much as possible 
              sign_out(user_2)
              sign_in(user)
              visit culminate_path(user_2.confirmation_key) 
           end

           it_should_behave_like 'a failed confirmation_attempt'

       end 

       scenario 'then signs out and attempts confirmation by following the confirmation link in his email' do
           let(:user_2) { FactoryGirl.build(:user) }

           before do
              sign_out(user)
              visit culminate_path(user.confirmation_key) 
           end

           describe 'the page' do
               subject { page }

               it_should_behave_like 'sign_in_page'

               it{ should have_selector '.alert-error', text: "Sorry, you need to be signed in inorder to attempt confirmation!" }
           end


           describe 'the instance' do
                it_should_behave_like 'a confirmed instance', user
           end

           scenario 'then signs in and visits link again' do
               before do   
                   sign_in(user)
                   visit culminate_path(user.confirmation_key) 
               end 

               it_should_behave_like 'a succeeded confirmation attempt'

           end
       end 
    end
end

更容易关注但不能干:

describe 'user spec', js: true do
    before { visit root_path }
    let(:user){ FactoryGirl.build(:user) }       
    let(:user_2) { FactoryGirl.build(:user) }

    # helper methods and shared examples hidden for brevity

    scenario 'user signs up' do
        before do
           register_user(user)
        end

       describe 'the page' do
           subject { page }

           it_should_behave_like 'profile_page' # shared example that tests content and title. 
           it{ should have_selector '.alert-success', text: "welcome to the site, #{user.user_name}! Please check your inbox for a confirmation email. You won't be able to do anything untill you confirm your account!" }
           it{ should have_selector 'h4', text: user.email }
           it{ should have_selector 'h4', text: user.user_name }
           it{ should have_selector 'h4', text: 'unconfirmed' }

       end   


       describe 'the instance' do
           subject { reload_user(user.user_name)} #is this the best way to do this? I want to do `user.reload` but I can't reload user because the user is built in memory (doesn't go through ActiveRecord), and so doesn't have an id

           its(:confirmation_key_created_at){ should eq Time.now } 
           its(:confirmation_key){ should be_base64 } # my own matcher. No, I'm not an autistic maths genius, just testing it's 64 characters long ;)
           its(:state){ should eq 'unconfirmed' }
       end

       describe 'last email' do
          subject { last_email }
          # using email-spec matchers https://github.com/bmabey/email-spec

          it { should have_subject "Hi #{user.user_name}, confirmation link enclosed!" }              
          it { should deliver_to user.email }
          it { should have_body_text user.confirmation_key }

       end
    end

    scenario 'a user signs up and then attempts confirmation by submitting a made up confirmation key' do

           before do
              register_user(user)
              visit culminate_path(SecureRandom.urlsafe_base64) 
           end

           it_should_behave_like 'a failed confirmation_attempt'

    end 

    scenario 'a user signs up and then attempts confirmation by following a confirmation link for a different user' do

           before do
              register_user(user_2)  
              sign_out(user_2)
              register_user(user)
              visit culminate_path(user_2.confirmation_key) 
           end

           it_should_behave_like 'a failed confirmation_attempt'

    end

    scenario 'then signs in and visits link again' do
           before do   
               sign_in(user)
               visit culminate_path(user.confirmation_key) 
           end 

           it_should_behave_like 'a succeeded confirmation attempt'
    end

    scenario 'a user signs out and then attempts confirmation by following the confirmation link in his email' do
       before do
          register(user)
          sign_out(user)
          visit culminate_path(user.confirmation_key) 
       end

       describe 'the page' do
           subject { page }

           it_should_behave_like 'sign_in_page'

           it{ should have_selector '.alert-error', text: "Sorry, you need to be         signed in inorder to attempt confirmation!" }
       end


       describe 'the instance' do
            it_should_behave_like 'an unconfirmed instance', user
       end
    end
end

1 个答案:

答案 0 :(得分:2)

我认为你是正确的,以消除大部分重复。但是,我不会使用嵌套方案,因为它们需要读者在文件中跳转。我也不会使用共享示例,因为它们会模糊控制流程。

我做这样的事情:

describe "user signup and confirmation" do

  let(:user) { # whatever }
  before do
    register_user
    visit root_path
  end

  scenario "user signs up" do
    page_should_show_that_user_is_not_confirmed
    user_should_not_be_confirmed
    # Write assertions here to assert that confirmation email was sent
    confirmation_url = confirmation_url_from_email
    visit confirmation_url
    page_should_show_that_user_is_confirmed
    user_should_be_confirmed
  end

  scenario "user logs out before visiting confirmation URL" do
    # no need to do the same assertions as we did in the previous scenario before the visit
    confirmation_url = confirmation_url_from_email
    sign_out user
    visit confirmation_url
    page_should_show_that_user_is_confirmed
    user_should_be_confirmed
  end

  scenario "user attempts to sign up with an unknown confirmation key" do
    visit culminate_path("some garbage")
    page_should_show_that_user_is_not_confirmed
    user_should_not_be_confirmed
  end

  scenario "user attempts to sign up with the wrong confirmation key" do
    user2 = # whatever
    visit culminate_path(user2.confirmation_key)
    page_should_show_that_user_is_not_confirmed
    user_should_not_be_confirmed
  end

  def page_should_show_that_user_is_not_confirmed
    # Assertions
  end

  def page_should_show_that_user_is_confirmed
    # Assertions
  end

  def user_should_not_be_confirmed
    # Assertions
  end

  def user_should_be_confirmed
    # Assertions
  end

  def confirmation_url_from_email
    # Don't assert anything, just get the URL from the email
  end

end

它总体上相对可读,我认为只有足够的重复才能保持可读性。