RSpec 'Expect' Syntax and Idiomatic attribute spec

前端 未结 2 2073
名媛妹妹
名媛妹妹 2021-02-13 22:13

This should have an easy answer but I\'m struggling to find it (have checked RSpec documentation, EverydayRails Testing with RSpec, Google results). In my model specs I like to

2条回答
  •  轮回少年
    2021-02-13 22:57

    Myron Marston, one of the core RSpec committers, explains here that you should still use

    it { should be_cool }
    

    If you've disabled the should syntax, he offers a solution to alias expect_it to it:

    RSpec.configure do |c|
      c.alias_example_to :expect_it
    end
    
    RSpec::Core::MemoizedHelpers.module_eval do
      alias to should
      alias to_not should_not
    end
    

    With this in place, you could write this as:

    describe User do
      expect_it { to be_valid }
    end
    

提交回复
热议问题