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 に答える