简体   繁体   中英

Exclamation mark used with assert method in its parameters

Okay this has been lingering in my head for quite a while now. In ruby on rails unit testing there is an exclamation mark with the assert method. Here is an example

test "No empty values to be inserted" do 
   product = Produce.new
   assert !product.save
end

Let me know the function of the exclamation mark. Quick replies appreciated. Thanks.

! is Logical negation.

  • If product.save is truthy (that is, neither nil nor false), !product.save returns false.
  • If product.save is falsy (that is, either nil or false), !product.save returns true.

Therefore, assert !product.save means that product.save must return falsy for the test to pass.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM