2016-04-04 31 views
3

新しい構文に古いRSpecの構文を変換し、彼らはこの持っている:例ではは、私が<a href="http://apionrails.icalialabs.com/book/chapter_two" rel="nofollow">APIonRails tutorial</a>を使用しています

require 'spec_helper' 

describe ApiConstraints do 
    let(:api_constraints_v1) { ApiConstraints.new(version: 1) } 
    let(:api_constraints_v2) { ApiConstraints.new(version: 2, default: true) } 

    describe "matches?" do 

    it "returns true when the version matches the 'Accept' header" do 
     request = double(host: 'api.marketplace.dev', 
         headers: {"Accept" => "application/vnd.marketplace.v1"}) 
     api_constraints_v1.matches?(request).should be_true 
    end 

    it "returns the default version when 'default' option is specified" do 
     request = double(host: 'api.marketplace.dev') 
     api_constraints_v2.matches?(request).should be_true 
    end 
    end 
end 

を、私はこれは古い構文を使用していることを理解するようになりました。新しい構文にこれを変換するには

、私はこれしようとしています:

require 'rails_helper' 

describe ApiConstraints do 
    let(:api_constraints_v1) { ApiConstraints.new(version: 1) } 
    let(:api_constraints_v2) { ApiConstraints.new(version: 2, default: true) } 

    describe "matches?" do 
    it "returns true when the version matches the 'Accept' header" do 
     request = double(host: 'api.localhost:3000', 
       headers: {"Accept" => "application/vnd.marketplace.v1"}) 
     expect(request).to match(api_constraints_v1) 
    end 

    it "returns the default version when 'default' option is specified" do 
     request = double(host: 'api.localhost:3000') 
     expect api_constraints_v2.matches?(request).to_be true 
    end 
    end 

end 

これは私が取得していますエラー:

Failures: 

    1) ApiConstraints matches? returns true when the version matches the 'Accept' header 
    Failure/Error: expect(request).to match(api_constraints_v1) 
     expected #<RSpec::Mocks::Double:0x3feeedaf60c4 @name=nil> to match #<ApiConstraints:0x007fddde50f9b0 @version=1, @default=nil> 
     Diff: 
     @@ -1,2 +1,2 @@ 
     -#<ApiConstraints:0x007fddde50f9b0 @default=nil, @version=1> 
     +#<RSpec::Mocks::Double:0x3feeedaf60c4 @name=nil> 

    # ./lib/spec/api_constraints_spec.rb:11:in `block (3 levels) in <top (required)>' 

    2) ApiConstraints matches? returns the default version when 'default' option is specified 
    Failure/Error: expect api_constraints_v2.matches?(request).to_be true 
    NoMethodError: 
     undefined method `to_be' for true:TrueClass 
     Did you mean? to_enum 
         to_s 
    # ./lib/spec/api_constraints_spec.rb:16:in `block (3 levels) in <top (required)>' 

Finished in 0.0045 seconds (files took 6.52 seconds to load) 
2 examples, 2 failures 

Failed examples: 

rspec ./lib/spec/api_constraints_spec.rb:8 # ApiConstraints matches? returns true when the version matches the 'Accept' header 
rspec ./lib/spec/api_constraints_spec.rb:14 # ApiConstraints matches? returns the default version when 'default' option is specified 

これを引き起こしている可能性がありますか?マイGemfile.lockパー

編集1

、これらは私の関連する宝石のバージョンです:私は同じことを達成することができるように使用すべき正しい新しい構文で何

rspec (3.1.0) 
    rspec-core (~> 3.1.0) 
    rspec-expectations (~> 3.1.0) 
    rspec-mocks (~> 3.1.0) 
rspec-core (3.1.7) 
    rspec-support (~> 3.1.0) 
rspec-expectations (3.1.2) 
    diff-lcs (>= 1.2.0, < 2.0) 
    rspec-support (~> 3.1.0) 
rspec-mocks (3.1.3) 
    rspec-support (~> 3.1.0) 
rspec-rails (3.1.0) 
    actionpack (>= 3.0) 
    activesupport (>= 3.0) 
    railties (>= 3.0) 
    rspec-core (~> 3.1.0) 
    rspec-expectations (~> 3.1.0) 
    rspec-mocks (~> 3.1.0) 
    rspec-support (~> 3.1.0) 
rspec-support (3.1.2) 
rubyzip (1.2.0) 
selenium-webdriver (2.53.0) 
    childprocess (~> 0.5) 
    rubyzip (~> 1.0) 
    websocket (~> 1.0) 
shellany (0.0.1) 
shoulda (3.5.0) 
    shoulda-context (~> 1.0, >= 1.0.1) 
    shoulda-matchers (>= 1.4.1, < 3.0) 
shoulda-context (1.2.1) 
shoulda-matchers (2.8.0) 
    activesupport (>= 3.0.0) 

元のコードは達成されますか?

+0

'expect request.to eq(api_constraints_v1)' - > 'expect(request).to eq(api_constraints_v1)' –

答えて

4

api_constraints_v1.matches?(request).should be_true 

へ0
expect(api_constraints_v1.matches?(request)).to be_truthy 

または

expect(api_constraints_v1.matches?(request)).to be(true) 

だけブール値が返されることを期待する場合

+0

パーフェクト。これはまさに私が探していたものです。どうもありがとう! – marcamillion

+1

多くの古い構文仕様を変換するには、[transpec](https://github.com/yujinakayama/transpec)を使うことを強くお勧めします。 –

1

変更

expect request.to eq(api_constraints_v1) 

expect(request).to eq(api_constraints_v1) 

およびその他の仕様で同じ問題...に

expect

あなたは期待が起こっている事を与える方法であり、オン(つまり request)...

request方法から結果 - あなたはそれにtoを呼び出す...

あなたが前にそれを持っていた方法は...あなたはexpectにその結果を渡し、その後requesttoを呼び出し最初です。 ...つまり、問題をグループ化;)元のテストの変更で

+0

本当に疑問に思うのは、 'expect(request).to be(api_constraints_v1)'です。 'api_constraints_v1.matches?(request).should be_true'と同じことです。私はそれが疑わしい。 古い構文の期待値の新しい構文バージョンは何ですか? – marcamillion

+0

カッコを付け加えると、 'should 'に与えられた部分がより明確に示されます(古い部分のためのベストプラクティスではありませんが、'(api_constraints_v1.matches?(request)期待する)。この場合(あなたのAPI制約が常に正規表現であると仮定して)、おそらくそれを 'expect(request).to match(api_constraints_v1)'に変更することができます。注:常に 'expect'構文で括弧を使います。それらはオプションではありません。 –

+0

ここにあるドキュメントの 'match'マッチャーの詳細は次の通りです:https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/match-matcher –

0

私は自分のrails-api-baseのプロジェクトで同じApiConstraintsを使用しました、あなたはAcceptヘッダーを指定せずにデフォルト以外のバージョンでmatches?をしようとすると、それがクラッシュします。

私は、次の試験(クラッシュ)を追加しました:

it 'returns false when not default and no Accept header' do 
    request = double(host: 'api.marketplace.dev') 
    expect(api_constraints_v1.matches?(request)).to be false 
end 

をそして私はApiConstraintsを固定:

def matches?(req) 
    @default || 
    (req.respond_to?('headers') && 
    req.headers.key?('Accept') && 
    req.headers['Accept'].include?("application/vnd.marketplace.v#{@version}")) 
end 

はそれが役に立てば幸い!

関連する問題

 関連する問題