0

在 user_spec.rb 文件中,我有这些相同的上下文,我想用干法重新编码。

    context 'when website adress starts with ' do 
      it 'http://, it should validate length of website' do
        @profile.website = "x" * 393 # with appending http to website url its length will be 400.
        assert @profile.save
      end
      it 'http://, it should not validate length of website' do 
        @profile.website = "x" * 394 # with appending http to website url its length will be 401.It should be failed.
        assert !@profile.save
      end
      it 'https://, it should validate length of website' do
        @profile.website = "https://" + "x" * 392 # with appending http to website url its length will be 400.
        assert @profile.save
      end
      it 'https://, it should not validate length of website' do 
        @profile.website = "https://" + "x" * 393 # with appending http to website url its length will be 401.It should be failed.
        assert !@profile.save
      end
    end

  context 'when blog adress starts with ' do 
    it 'http://, it should validate length of blog' do
      @profile.blog = "x" * 393 # with appending http to blog url its length will be 400.
      assert @profile.save
    end
    it 'http://, it should not validate length of blog' do 
      @profile.blog = "x" * 394 # with appending http to blog url its length will be 401.It should be failed.
      assert !@profile.save
    end
    it 'https://, it should validate length of blog' do
      @profile.blog = "https://" + "x" * 392 # with appending http to blog url its length will be 400.
      assert @profile.save
    end
    it 'https://, it should not validate length of blog' do 
      @profile.blog = "https://" + "x" * 393 # with appending http to blog url its length will be 401.It should be failed.
      assert !@profile.save
    end
  end

有什么办法可以这样写吗?我想同时用2个方法。当我在下面编写代码并调用时,should_validate_length_of('website')我有 undefined local variable or methodshould_validate_length_of'` 错误

def should_validate_length_of(dummy)
    context 'when website adress starts with ' do 
      it 'http://, it should validate length of website' do
        @profile.dummy = "x" * 393 # with appending http to website url its length will be 400.
        assert @profile.save
      end
      it 'http://, it should not validate length of website' do 
        @profile.dummy = "x" * 394 # with appending http to website url its length will be 401.It should be failed.
        assert !@profile.save
      end
      it 'https://, it should validate length of website' do
        @profile.dummy = "https://" + "x" * 392 # with appending http to website url its length will be 400.
        assert @profile.save
      end
      it 'https://, it should not validate length of website' do 
        @profile.dummy = "https://" + "x" * 393 # with appending http to website url its length will be 401.It should be failed.
        assert !@profile.save
      end
    end
  end
4

1 回答 1

0

看看这段代码,我通过创建一个方法来减少代码量

需要'spec_helper'

describe SomeClass do
  let(:inner_app) { ->(env){[200, {'Content-Type' => 'text/plain'}, ['All good!']]} }
  let(:app) { SomeClass.new(inner_app) }

  class << self
    def method_tests(m, http_status, response_status)
      it "returns a #{http_status} status"  do
        status, _, _ = app.send(m, response)
        expect(status).to eq(http_status)
      end
      it "has application/json content type headers" do
        _, headers, _ = app.send(m, response)
        expect(headers).to include({'Content-Type' => 'application/json'})
      end
      it "returns a formatted body with status #{response_status}" do
        _, _, body = app.send(m, response)
        expect(body.first).to eq(response.merge!(status: response_status).to_json)
      end
    end
  end

  describe "#success" do
    method_tests(:success, 200, :success)
  end

  describe "#unauthorized" do
    method_tests(:unauthorized, 401, :error)
  end

  describe "#bad_request" do
    method_tests(:bad_request, 400, :error)
  end
end

方法本身必须是类方法。尽管我的测试与您的测试不同,但概念仍然相同。:)

于 2013-05-10T15:34:52.047 回答