{"id":3663,"date":"2015-04-08T23:37:41","date_gmt":"2015-04-08T23:37:41","guid":{"rendered":"http:\/\/webninjataylor.com\/library\/?p=3663"},"modified":"2015-04-08T22:26:20","modified_gmt":"2015-04-09T02:26:20","slug":"ruby-on-rails-testing-configuration-and-forms-in-rails","status":"publish","type":"post","link":"https:\/\/webninjataylor.com\/library\/ruby-on-rails-testing-configuration-and-forms-in-rails\/","title":{"rendered":"Ruby on Rails: Testing, Configuration, and Forms in Rails"},"content":{"rendered":"<ul>\n<li>There\u2019s a default testing framework in Ruby 1.9+\n<ul>\n<li>Borrows from Test::Unit and RSpec<\/li>\n<li>Choose one or the other to write <strong>all<\/strong> your tests in; don&#8217;t use both &#8230; be\u00a0consistent<\/li>\n<\/ul>\n<\/li>\n<li>Rails provides testing support out of the box\n<ul>\n<li>Test runner (via Rake)<\/li>\n<li>Testing models against the database using fixtures<\/li>\n<li>Functional testing controllers (e.g. that the create action in a controller saves a new model)<\/li>\n<li>Integration tests (e.g. testing application flow through multiple controllers)<\/li>\n<li>Support for testing mailers and helpers<\/li>\n<\/ul>\n<\/li>\n<li>Tests are run against a test database<\/li>\n<li>Isolates testing from other environments (e.g. development and production)<\/li>\n<li>After you run a migration, run:\u00a0<span class=\"code\">rake db:test:prepare<\/span><\/li>\n<li>Rails automatically:\n<ul>\n<li>Cleans the test database before each test<\/li>\n<li>Starts a database transaction before each test<\/li>\n<li>Rolls back the transaction after each test to ensure each test has a known state\n<ul>\n<li>Is much faster (since changes don&#8217;t hit disk)<\/li>\n<li>Keeps tests isolated (consistent state before &amp; after tests)<\/li>\n<li>Makes tests easier to implement (no messy data setup, teardown, or consistency checks needed)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>Not really &#8220;unit&#8221; testing since testing against a live database is more integration testing than unit testing<\/li>\n<li><span class=\"code\">rake &#8211;all -T<\/span> to list all tasks even if it doesn&#8217;t have a description such as test:models<\/li>\n<li>Fixtures populate the test database<\/li>\n<li>Rails is configured through a few .rb and .yml files<\/li>\n<li>Database connection information is stored in database.yml<\/li>\n<li>7 standard <strong>REST<\/strong>ful routes in Rails\n<ul>\n<li>index, new, show, create, edit, update, destroy<\/li>\n<li>Rails calls it <strong>Resource Routing<\/strong><\/li>\n<li><strong>Re<\/strong>presentational <strong>S<\/strong>tate <strong>T<\/strong>ransfer\n<ul>\n<li>An architectural style described by Roy Fielding in his 2000 Ph.D. thesis<\/li>\n<li>Key properties:\n<ul>\n<li>Resources operated on by actions (e.g. HTTP PUT)<\/li>\n<li>Actions are constrained to limited but consistent set<\/li>\n<li>Stateless (all necessary state is supplied in actions)<\/li>\n<li>Scalability (due to stateless nature)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li><strong>Partials<\/strong> are a way to extract shared view code in templates\n<ul>\n<li>Partial names begin with an underscore, e.g. \/app\/views\/books\/_form.html.erb<\/li>\n<li>When you render partials, you exclude the underscore, e.g. &#8216;form&#8217;<\/li>\n<li>You can pass arguments to partials\n<ul>\n<li>Typical use is to pass a collection of models<\/li>\n<li>Partial is rendered once for each object in collection<\/li>\n<li>You can reference the object from within the partial<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>Resources<\/h2>\n<ul>\n<li><a href=\"http:\/\/guides.rubyonrails.org\/routing.html\" target=\"_blank\">Rails Routing<\/a><\/li>\n<li><a href=\"http:\/\/guides.rubyonrails.org\/testing.html\" target=\"_blank\">Testing Rails<\/a><\/li>\n<li><a href=\"http:\/\/guides.rubyonrails.org\/form_helpers.html\" target=\"_blank\">Form helpers<\/a><\/li>\n<li><a href=\"http:\/\/guides.rubyonrails.org\/action_controller_overview.html\" target=\"_blank\">Controllers<\/a><\/li>\n<li><a href=\"http:\/\/en.wikipedia.org\/wiki\/Representational_state_transfer\" target=\"_blank\">Representational State Transfer (REST)<\/a><\/li>\n<li><a href=\"http:\/\/guides.rubyonrails.org\/layouts_and_rendering.html#using-partials\" target=\"_blank\">Using Partials<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>There\u2019s a default testing framework in Ruby 1.9+ Borrows from Test::Unit and RSpec Choose one or the other to write all your tests in; don&#8217;t use both &#8230; be\u00a0consistent Rails provides testing support out of the box Test runner (via Rake) Testing models against the database using fixtures Functional testing controllers (e.g. that the create [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[144],"tags":[152],"class_list":["post-3663","post","type-post","status-publish","format-standard","hentry","category-web-shots","tag-ruby"],"_links":{"self":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3663","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/comments?post=3663"}],"version-history":[{"count":8,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3663\/revisions"}],"predecessor-version":[{"id":3668,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3663\/revisions\/3668"}],"wp:attachment":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/media?parent=3663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/categories?post=3663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/tags?post=3663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}