ruby on rails - Difference between expect and is_expected.to in Rspec -
what difference between keywords.
in following example, using expect
passed test, while is_expected.to
failed it.
it { expect validate_uniqueness_of(:access_token) }
it { is_expected.to validate_uniqueness_of(:access_token) }
testing class user
, generated devise
class user < activerecord::base devise :lockable, :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable validates :access_token, uniqueness: true before_validation :generate_access_token!, on: :create def generate_access_token! begin self.access_token = devise.friendly_token end while user.find_by(access_token: self.access_token) end end
is_expected_to
shorter version of writing
expect(subject).to
your first spec passes because not testing @ all.
the second spec fails because there no uniqueness validation. although code handling duplicates (but same race condition validation), doing in different way: generates new token, rather reporting error. validation matchers typically work checking object's errors
hash, , code doesn't set spec fails.
Comments
Post a Comment