简体   繁体   中英

Rails3 - Testing - what is the relationship errors and invalid?

In the following code, do the errors support invalid?

in other words is invalid? true or contingent upon the list of errors being true

or is invalid? working on its own?

test "product attributes must not be empty" do product = Product.new

assert product.invalid? assert product.errors[:title].any? assert product.errors[:description].any?

assert product.errors[:price].any?

assert product.errors[:image_url].any? end

Also, may I assume that:

Functional Testing (for controllers) perform at run time for the user, while Unit Testing (for Models / Database) are for use during development

THANKS! strong text

The tests are run during development, to try and ensure the code you produce is as error-free as possible.

Unit tests check small units of code (eg a single model), while functional tests check "functions" that take several steps (such as the "signing" up process).

The valid? function essentially makes the model go through the defined validators, and checks whether there where errors. In other words, if the @user.errors array (for example) contains entires, valid? will return false.

But once again, tests are used to check you're developing your code properly, and will NOT run in production.

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