Checking ActiveRecord Associations in RSpec

前端 未结 4 1449
长情又很酷
长情又很酷 2021-02-05 10:16

I am learning how to write test cases using Rspec. I have a simple Post Comments Scaffold where a Post can have many Comments. I am testing this using Rspec. How should i go abo

4条回答
  •  南旧
    南旧 (楼主)
    2021-02-05 10:36

    Most people don't test the associations, as Rails already has unit tests to make sure those methods work correctly. If you are doing something complex, like involving a proc or something, you might want to explicitly test it. Usually you can do this by just doing

    a = Post.new
    a.comments << Comment.new
    assert a.save
    assert a.comments.size == 1
    

    or something akin to that.

提交回复
热议问题