shoulda-matchers RSpec expect syntax

前端 未结 2 1686
伪装坚强ぢ
伪装坚强ぢ 2021-01-30 17:49

What is the correct format for using shoulda-matchers and RSpec\'s new expect syntax?

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-30 18:29

    I'll suplement the answer of @peter-alfvin. In case you test the model and its migration themselves with shoulda-matchers you can't use :expect outside of it block, so can't write:

    RSpec.describe ModelName, type: :model do
       expect(subject).to belong_to(:user)
    end
    

    And you will get the expection:

    `expect` is not available on an example group (e.g. a `describe` or `context` block).
    

    but correct version is:

    RSpec.describe ModelName, type: :model do
       it { expect(subject).to belong_to(:user) }
    end
    

提交回复
热议问题