2014-09-30 16 views
5

Rspec 3'ü öğrenme. Eşleştiricilerle ilgili bir sorum var. bir i amortisman uyarı ile geçen dışarı yorumladı ise kod bir hataya neden olan Neden ben takip ediyorum öğreticiRspec 3 vs Rspec 2 eşleşmeleri

describe Team do 

    it "has a name" do 
    #Team.new("Random name").should respond_to :name 
    expect { Team.new("Random name") }.to be(:name) 
    end 


    it "has a list of players" do 
    #Team.new("Random name").players.should be_kind_of Array 
    expect { Team.new("Random name").players }.to be_kind_of(Array) 
    end 

end 

RSpec 2. dayanmaktadır.

Hata

Failures: 

    1) Team has a name 
    Failure/Error: expect { Team.new("Random name") }.to be(:name) 
     You must pass an argument rather than a block to use the provided matcher (equal :name), or the matcher must implement `supports_block_expectations?`. 
    # ./spec/team_spec.rb:7:in `block (2 levels) in <top (required)>' 

    2) Team has a list of players 
    Failure/Error: expect { Team.new("Random name").players }.to be_kind_of(Array) 
     You must pass an argument rather than a block to use the provided matcher (be a kind of Array), or the matcher must implement `supports_block_expectations?`. 
    # ./spec/team_spec.rb:13:in `block (2 levels) in <top (required)>' 
+1

Kontrol bu [cevap] (http://stackoverflow.com/questions/19960831/rspec-expect-vs-expect-with-block-whats-the-difference) _why? _ –

cevap

6

Sen o testler için normal bir parantez kullanmalıdır:

expect(Team.new("Random name")).to eq :name 

Eğer süslü parantez kullanmak

, bir kod bloğu geçiyoruz. rspec3 ama, Sen yürütme sonucuna yerine, bu blokun yürütülmesi ile ilgili bazı beklentileri koymak, böylece anlamına gelir, örneğin

expect { raise 'hello' }.to raise_error 

DÜZENLEME: bu test Team.new olarak, başarısız olacağını ancak

Not bir nesneyi döndürür, bir sembolü değil. geçene böylece testinizi değiştirebilirsiniz:

expect(Team.new("Random name")).to respond_to :name 

# or 

expect(Team.new("Random name").name).to eq "Random name" 
+0

Bunun için bir hata alıyorum. https://gist.github.com/vezu/85661922adda6a877b48. Açıklama için teşekkür ederim. – Benjamin

+1

@Benjamin - 'Team.new' ifadesinin bir sembol değil, bir nesne döndürmesi beklendiğini söyleyebilirim. Cevap güncellendi. – BroiSatse