<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Techy Things &#187; ruby-on-rails</title>
	<atom:link href="https://tech.yipp.ca/category/ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>https://tech.yipp.ca</link>
	<description>Just another Yipp.ca Blogs site</description>
	<lastBuildDate>Thu, 01 May 2025 18:06:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>Unit tests in Ruby on Rails</title>
		<link>https://tech.yipp.ca/ruby-on-rails/unit-tests-ruby-rails/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=unit-tests-ruby-rails</link>
		<comments>https://tech.yipp.ca/ruby-on-rails/unit-tests-ruby-rails/#comments</comments>
		<pubDate>Sat, 04 Feb 2017 16:59:11 +0000</pubDate>
		<dc:creator><![CDATA[frank]]></dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby-on-rails]]></category>

		<guid isPermaLink="false">http://tech.yipp.ca/?p=2740</guid>
		<description><![CDATA[<p>How to write and run your first unit test for your ruby on rails webapp Fist time setup $ rake db:test:load This will recreate a new test DB based ont the current schema! Tasks&#46;&#46;&#46;</p>
<p>The post <a rel="nofollow" href="https://tech.yipp.ca/ruby-on-rails/unit-tests-ruby-rails/">Unit tests in Ruby on Rails</a> appeared first on <a rel="nofollow" href="https://tech.yipp.ca">Techy Things</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>How to write and run your first unit test for your ruby on rails webapp</p>
<h2 class="line number2 index1 alt1">Fist time setup</h2>
<div class="line number3 index2 alt2"><code class="plain plain">$ rake db:test:load</code></div>
<div class="line number3 index2 alt2">This will recreate a new test DB based ont the current schema!</div>
<div class="line number3 index2 alt2"></div>
<div class="line number3 index2 alt2">
<table>
<tbody>
<tr>
<th>Tasks</th>
<th>Description</th>
</tr>
<tr>
<td><tt>rake db:test:clone</tt></td>
<td>Recreate the test database from the current environment’s database schema</td>
</tr>
<tr>
<td><tt>rake db:test:clone_structure</tt></td>
<td>Recreate the test database from the development structure</td>
</tr>
<tr>
<td><tt>rake db:test:load</tt></td>
<td>Recreate the test database from the current <tt>schema.rb</tt></td>
</tr>
<tr>
<td><tt>rake db:test:prepare</tt></td>
<td>Check for pending migrations and load the test schema</td>
</tr>
<tr>
<td><tt>rake db:test:purge</tt></td>
<td>Empty the test database.</td>
</tr>
</tbody>
</table>
</div>
<div class="info"></div>
<h2 class="info">Writing a test</h2>
<p>&nbsp;</p>
<div class="info"></div>
<h2 class="info">Running your tests</h2>
<p>$&gt; <code class="plain plain">ruby -Itest test/unit/recette_test.rb</code></p>
<table>
<tbody>
<tr>
<th>Tasks</th>
<th>Description</th>
</tr>
<tr>
<td><tt>rake test</tt></td>
<td>Runs all unit, functional and integration tests. You can also simply run <tt>rake</tt> as the <em>test</em> target is the default.</td>
</tr>
<tr>
<td><tt>rake test:benchmark</tt></td>
<td>Benchmark the performance tests</td>
</tr>
<tr>
<td><tt>rake test:functionals</tt></td>
<td>Runs all the functional tests from <tt>test/functional</tt></td>
</tr>
<tr>
<td><tt>rake test:integration</tt></td>
<td>Runs all the integration tests from <tt>test/integration</tt></td>
</tr>
<tr>
<td><tt>rake test:plugins</tt></td>
<td>Run all the plugin tests from <tt>vendor/plugins/*/**/test</tt> (or specify with <tt>PLUGIN=_name_</tt>)</td>
</tr>
<tr>
<td><tt>rake test:profile</tt></td>
<td>Profile the performance tests</td>
</tr>
<tr>
<td><tt>rake test:recent</tt></td>
<td>Tests recent changes</td>
</tr>
<tr>
<td><tt>rake test:uncommitted</tt></td>
<td>Runs all the tests which are uncommitted. Supports Subversion and Git</td>
</tr>
<tr>
<td><tt>rake test:units</tt></td>
<td>Runs all the unit tests from <tt>test/unit</tt></td>
</tr>
</tbody>
</table>
<h2>Troubleshooting</h2>
<p>If you are on an large existing project and are just starting writing your tests you may face a couple of walking on broken glass debugging.</p>
<h3>Problem: DELETE FROM non-existing tables</h3>
<p>ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: friends: DELETE FROM "friends"</p>
<p><a href="http://tech.yipp.ca/files/2017/02/ruby-on-rails-tests-delete-from-unexisting-table.png"><img class="alignnone wp-image-2742 size-large" src="http://tech.yipp.ca/files/2017/02/ruby-on-rails-tests-delete-from-unexisting-table-800x186.png" alt="ruby-on-rails-tests-delete-from-unexisting-table" width="720" height="167" /></a></p>
<p>Let's check the log test.log</p>
<p>...<br />
[1m[36mFixture Delete (0.0ms)[0m [1mDELETE FROM "friendrequests"[0m<br />
[1m[35mFixture Delete (0.0ms)[0m DELETE FROM "friends"<br />
SQLite3::SQLException: no such table: friends: DELETE FROM "friends"<br />
...</p>
<p><span style="color: #ff6600;"><strong>The solution is surprisingly to remove a file from the test/fixture folder</strong></span></p>
<p><a href="http://tech.yipp.ca/files/2017/02/ruby-on-rails-tests-delete-from-unexisting-table-solved.png"><img class="alignnone size-full wp-image-2743" src="http://tech.yipp.ca/files/2017/02/ruby-on-rails-tests-delete-from-unexisting-table-solved.png" alt="ruby-on-rails-tests-delete-from-unexisting-table-solved" width="654" height="501" /></a></p>
<p>&nbsp;</p>
<h3>Problem: INSERT INTO a table that has no column named 'XYZ'</h3>
<p><a href="http://tech.yipp.ca/files/2017/02/ruby-on-rails-tests-insert-table-has-no-column-named.png"><img class="alignnone size-large wp-image-2747" src="http://tech.yipp.ca/files/2017/02/ruby-on-rails-tests-insert-table-has-no-column-named-800x191.png" alt="ruby-on-rails-tests-insert-table-has-no-column-named" width="720" height="172" /></a></p>
<p>&nbsp;</p>
<p><span style="color: #ff6600;"><strong>This is due to your fixture deriving from your models schema. The solution is to fix your fixtures files.</strong></span></p>
<p>In my case the solution was to change this for that :</p>
<p><a href="http://tech.yipp.ca/files/2017/02/ruby-on-rails-test-offers-yml.png"><img class="alignnone wp-image-2749 size-full" src="http://tech.yipp.ca/files/2017/02/ruby-on-rails-test-offers-yml.png" alt="ruby-on-rails-test-offers-yml" width="867" height="290" /></a></p>
<p>&nbsp;</p>
<div class="line number3 index2 alt2">
<div class="info">
<h2>Version used for this article</h2>
<p>Rails 4<br />
Ruby 2.1.9p490 (2016-03-30 revision 54437)</p>
</div>
<h2>Further suggested reading</h2>
<p>https://everydayrails.com/2012/03/12/testing-series-intro.html</p>
</div>
<p>The post <a rel="nofollow" href="https://tech.yipp.ca/ruby-on-rails/unit-tests-ruby-rails/">Unit tests in Ruby on Rails</a> appeared first on <a rel="nofollow" href="https://tech.yipp.ca">Techy Things</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://tech.yipp.ca/ruby-on-rails/unit-tests-ruby-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
