1. Rails provides a simple way to do data validation. The model layer is the gatekeeper between the world of code and the database. If a model checks the data before writing to the database, then the database will be protected from bad data. The source code is in app/models/xxx.rb. An example of validation is:
validates :title, :description, :image_url, presence: true
2. Rails provides a test framework. The framework is in test directory. You can test the entire framework by:
$ rake test
Unit test cases can be created in test/models directory, and you can test a particular test case file, e.g.:
$ rake test:models
3. We use assertion to tell the framework whether our code passes or fails. The simplest assertion is the method assert(), which expects its argument to be true, e.g.:
assert product.invalid?, "DEBUG COMMENTS"
assert product.errors[:title].any?
4. Fixture is a specification of the initial contents of a model under test. Fixture data is specified in the test/fixture directory. The name of the fixture file is significant. The base name of the file must match the name of a database table. The default for the test is to load ll fixtures before running the test, but again you can control which fixtures to load by specifying the following line in the test/models/xxx_test.rb:
class ProductTest < ActiveSupport::TestCase
fixtures :products
...
end
The name of the fixture file determines the table that is loaded, so using :products will cause the products.yml fixture file to be used.
5. There are three databases that has been created in the configuration that scaffolding provides.
(1) db/development.sqlite3 will be the development database.
(2) db/test.sqlite3 is a test database.
(3) db/production.sqlite3 is the production database.
rake test command automatically gets a freshly initialized table in the test.sqlite3 database loaded from the fixtures we provide. Also we can do it separately by running rake db:test:prepare.
No comments:
Post a Comment