开发者

Test driven development: How do I not repeat myself on top of the tests?

I have this file as my starting point

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'
      res开发者_运维问答ponse.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

But you notice the line

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

This is my basic HTTP authentication and I need this on all my tests but i fee like there has got to be a better way of adding this before all my tests then copying and pasting on top of all the tests


describe PagesController do

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

  ...

end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜