2014-01-24 8 views
1

我正在做代碼學校的Rspec教程,並有一個這樣的示例代碼。`have`匹配器接受單數形式的項目嗎?

describe Zombie do 
    it 'increases the number of tweets' do 
    zombie = Zombie.new(name: 'Ash') 
    zombie.tweets.new(message: "Arrrgggggggghhhhh") 
    zombie.tweets.should have(1).tweets 
    end 
end 

它希望有一個鳴叫,所以我重寫have(1).tweetshave(1).tweet。然後它也可以工作。

但在documentation沒有跡象表明接受單數形式。

我想知道建議使用哪種方式,have(1).tweetshave(1).tweet

+1

我認爲複數形式是推薦的,因爲單數形式沒有在文檔 – emaillenin

+2

中提到'have(1).tweets'感覺不自然。由於Rspec試圖使測試儘可能可讀,所以我會在這裏親自使用單數形式。 – BroiSatse

回答

1

have(n)之後的方法調用是「語法糖」,可以是任何你想要的,如在https://www.relishapp.com/rspec/rspec-expectations/v/2-99/docs/built-in-matchers/have-n-items-matcher的Relish文檔以及早期版本中所描述的。

我所引用的2.99津津樂道頁因爲3.0,這一功能已被移動到一個單獨的rspec-collections_matcher寶石在https://github.com/rspec/rspec-collection_matchers你不會發現有3.0文檔中提到的匹配。

至於是否應該使用單數或複數形式的「tweet」,我相信有一種共識,就英語而言,單數形式代表這個特定案例的正確語法。對於沒有明確共識的案例,見https://meta.stackexchange.com/questions/165244/is-negative-one-plural

+0

這真是太棒了,謝謝。 –