Ruby on rails 3 测试驱动开发:我如何不在测试之上重复自己?

Ruby on rails 3 测试驱动开发:我如何不在测试之上重复自己?,ruby-on-rails-3,rspec,Ruby On Rails 3,Rspec,我把这个文件作为我的出发点 require 'spec_helper' describe PagesController do render_views describe "GET 'home'" do it "should be successful" do @request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("username:password") get 'home'

我把这个文件作为我的出发点

require 'spec_helper'

describe PagesController do
  render_views  
  describe "GET 'home'" do
    it "should be successful" do
      @request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("username:password")
      get 'home'
      response.should be_success
    end

    it "Should have the proper title" do
      @request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("username:password")
      get 'home'
      response.should have_selector( "title",
      :content => "Slacklog")
    end
  end

  describe "GET 'contact'" do
    it "should be successful" do
      @request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("username:password")
      get 'contact'
      response.should be_success
    end
  end

  describe "GET 'about'" do
    it "should be successful" do
      @request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("username:password")
      get 'about'
      response.should be_success
    end
  end

end
但是你注意到了那条线

      @request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("username:password")
这是我的基本HTTP身份验证,我在所有测试中都需要它,但我觉得必须有更好的方法在所有测试之前添加它,然后在所有测试之上复制和粘贴

describe PagesController do

  before(:each) do
    @request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("username:password")
  end

  ...

end