Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 24c2729e6e
Fetching contributors…

Cannot retrieve contributors at this time

491 lines (342 sloc) 28.64 kb
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="">
<title><![CDATA[Entreprevelopment adventures]]></title>
<link href="" rel="self"/>
<link href=""/>
<name><![CDATA[Luisa Lima]]></name>
<generator uri="">Octopress</generator>
<title type="html"><![CDATA[How I test part II - testing models]]></title>
<link href=""/>
<content type="html"><![CDATA[<h2>Testing models</h2>
<p>From <a href="">rails guides</a>:</p>
<p>&#8220;Ideally, you would like to include a test for everything which could possibly break. It’s a good practice to have at least one test for each of your validations and at least one test for every method in your model.&#8221;</p>
<p>Which leads me to&#8230;</p>
<h2>Why I use shoulda</h2>
<p>I think that the best thing is just to take a peek at <a href="">The shoulda cheat sheets</a>.</p>
<p>You actually get a guide of what to test by looking at the cheat sheet! It&#8217;s too bad that it&#8217;s not up to date (actually I think that the whole site is not up to date &#8211; even the gem doesn&#8217;t work anymore, which I think is too bad :-( )</p>
<p>Furthermore, it&#8217;s great for lazy people and it&#8217;s definitely easier on the eyes. Just check the following example:</p>
<div><script src=''></script>
<noscript><pre><code># without shoulda... tons of code :-)
it &quot;should belong to person&quot; do
Model.reflect_on_association(:person).macro.should == :belongs_to
# with shoulda... one-liner!
it{should belong_to(:person)}
<p>Convinced? ;-)</p>
<h2>What I test exactly with shoulda</h2>
<p>I go through the whole list of shoulda macros for models, and I just use and abuse them. Since the cheat sheet is not up to date, I created a new one (just for models for now) at:</p>
<div><script src=''></script>
<noscript><pre><code># updated from the original @
# just a subset -- models -- is included here. I'll update this, and create cheat sheets for others, as I go along.
# I marked the ones I added with NEW and also added the links to the corresponding code, as I think it's useful.
# Any comments/corrections are welcome!
# ================= Data and Associations =======================
it { should_not have_db_column(:admin).of_type(:boolean) }
it { should have_db_column(:salary).
with_options(:precision =&gt; 10, :scale =&gt; 2) }
it { should have_readonly_attributes(:password) }
it { should belong_to(:parent) }
it { should have_db_index(:id) } # NEW
it { should have_many(:friends) }
it { should have_many(:enemies).through(:friends) }
it { should have_many(:enemies).dependent(:destroy) }
it { should have_one(:god) }
it { should have_and_belong_to_many(:posts) }
it {should accept_nested_attributes_for(:user)} # NEW
# ================== Validation Matchers ============================
it { should validate_uniqueness_of(:keyword) }
it { should validate_uniqueness_of(:keyword).with_message(/dup/) }
it { should validate_uniqueness_of(:email).scoped_to(:name) }
it { should validate_uniqueness_of(:email).
scoped_to(:first_name, :last_name) }
it { should validate_uniqueness_of(:keyword).case_insensitive }
it { should validate_presence_of(:name) }
it { should validate_presence_of(:name).
with_message(/is not optional/) }
it { should validate_numericality_of(:age) }
it { should validate_format_of(:name).
with_message(/is not optional/) }
it { should validate_format_of(:name).
with_message(/is not optional/) }
it { should validate_acceptance_of(:eula) }
it { should ensure_length_of(:password).
is_at_most(20) }
it { should ensure_length_of(:name).
with_short_message(/not long enough/) }
it { should ensure_length_of(:ssn).
with_message(/is invalid/) }
it { should ensure_inclusion_of(:age).in_range(0..100) }
it { should_not allow_mass_assignment_of(:password) }
it { should allow_mass_assignment_of(:first_name) }
it { should validate_format_of(:first_name).with(&quot;Carl&quot;) }
it { should validate_confirmation_of(:password) } # NEW</code></pre></noscript></div>
<p>Yes, it&#8217;s a bit tedious. But (1) you make sure that you are actually testing all these details, which is extremely useful when you are constantly refactoring your code, and (2) it&#8217;s not that slow with the multiple cursors of Sublime Text 2. You just need to have a scaffold.</p>
<h2>What I test without shoulda</h2>
<p>Apart from model methods, is there that much to test without shoulda? I test:</p>
<li>Any model methods, extensively</li>
<li>Callbacks, extremely extensively</li>
<li>Some validations - but just to make sure I coded them right. See more of this perspective in <a href="">Testing model validations in rspec the short and sweet way</a></li>
<p>&#8230; and for now, that&#8217;s about it.</p>
<h2>Regarding generating fake data</h2>
<p>Like I mentioned before, I do use FactoryGirl extensively. Maybe that means that <a href="">my models are complicated</a>? I don&#8217;t know, but for now, I am sort of using good sense and stuff. Let&#8217;s see what happens. I do use <a href="">Faker</a> as well. Come to think about it, will do a post on that eventually.</p>
<h2>Random issues in testing models</h2>
<p>When everything seems to be absolutely correct in the models and in the tests and you are still getting unexplainable errors, do restart guard. I already spent several hours messing around with the tests because guard didn&#8217;t refresh the models. <a href="">There are automatic solutions</a>, but I didn&#8217;t feel like hacking away the spork/guard configurations again&#8230; yet.</p>
<h2>Random resources</h2>
<li><p><a href="">Everyday Rails: How I learned to test my Rails applications, Part 3: Model specs</a> - this has a very nice overview of the actual steps required to test models. Now I just cheat using shoulda, but it&#8217;s very nice and useful to get a</p></li>
<li><p><a href="">Testing model validations in rspec the short and sweet way</a> - it&#8217;s a bit outdated, but describes how to test validations in a short way.</p></li>
<hr />
<p>Again, as I already mentioned, I am a beginner to rails and, consequently, in testing rails, so I might be doing/saying terribly wrong things. Any input is very much appreciated :-)</p>
<title type="html"><![CDATA[Inspiring TED talk]]></title>
<link href=""/>
<content type="html"><![CDATA[<p>For anyone who works in business (especially startups), this is most probably (or most definitely) the most important TED talk that you&#8217;ll ever get to watch. Or the most important talk/piece of advice that you&#8217;ll ever get, I believe.</p>
<p><a href="">How great leaders inspire action</a></p>
<p>The fact is that this is what attracts me (and many people, probably) to startups. It&#8217;s just easier to find this sharing of common beliefs &#8211; and if it&#8217;s not, it&#8217;s the wrong startup.</p>
<p><strong>People don&#8217;t buy what you do, they buy why you do it.</strong></p>
<p>People buy what you believe in. Somehow this makes me believe in mankind a bit more :-)</p>
<title type="html"><![CDATA[Analytics js & a mash of interesting rails stuff]]></title>
<link href=""/>
<content type="html"><![CDATA[<h2>Analytics.js</h2>
<p>I don&#8217;t know whether this library is very common, but it definitely shows a lot of promise:</p>
<p><a href="">Segmentio&#8217;s Analytics.js</a></p>
<p>It provides provider-agnostic hooks to route to analytics services. They also have a hosted version where the analytics services can be changed without touching the code. Looks really clean and neat.</p>
<h2>Nice video: to_proc</h2>
<p><a href="">This video by CooperPress</a> explains in a very clear fashion how to use the &#8220;to_proc&#8221; feature of Ruby (and understand some weird code you might come across). It is also well worth it to subscribe to <a href="">their other videos</a>.</p>
<h2>Faster Rails partial rendering and caching</h2>
<p>I sure will try out <a href="">this gem (multi_fetch_fragments)</a> to optimize Rails when I get to that point of my work&#8230;</p>
<h2>Indexes in Rails databases</h2>
<p><a href="">Extremely good article by Tom Ward on using indexes in rails associations</a>. An excellent guide to determine what indexes your app should include (no more, no less).</p>
<title type="html"><![CDATA[How I test - Part I: Environment]]></title>
<link href=""/>
<content type="html"><![CDATA[<p>Not that I am an authority in testing&#8230; this is just a compilation of the so-called &#8220;best practices&#8221; that I&#8217;ve been seeing around, as well as some extra tips from my (very limited) experience. I&#8217;d love to hear the feedback from more experienced people&#8230; I&#8217;m going to do several of these as I go along.</p>
<h2>Gems I use</h2>
<p>I test using <a href="">rspec</a> and <a href="">shoulda</a>, which dramatically reduces the size of the tests. But beware &#8211; rspec is a DSL, and I think that I only got a bit more intuition on what I was exactly testing after doing some tests in Test::Unit. I also use <a href="">FactoryGirl</a> for setting up the test data, instead of fixtures. Last but not least, the <a href="">simplecov</a> gem is amazing, and although you shouldn&#8217;t rely on it to determine when to stop testing, it really is good to get a (very precise) sense of what&#8217;s covered and what&#8217;s not in their beautiful html reports.</p>
<h2>General tips</h2>
<p>When all your tests are red for no apparent reason, it&#8217;s because you were messing around with the db and then forgot to do</p>
<pre><code>rake db:test:clone</code></pre>
<p>Yup, the reason I am mentioning it here even though it seems completely obvious is because I am distracted and do that a lot&#8230; :-)</p>
<h2>Tips for running guard/spork in vagrant</h2>
<p>I use vagrant to keep my environment all neatly in one place. However, there are downsides. When running guard inside the guest OS, use</p>
<pre><code>bundle exec guard -p</code></pre>
<p>Otherwise, guard won&#8217;t detect changes in the guest filesystem.</p>
<h2>FactoryGirl tips</h2>
<p>You can keep all factories in one file, for me that is more intuitive (and also, I don&#8217;t have to keep opening files while I&#8217;m testing, it&#8217;s all in one place.)</p>
<p>To try FactoryGirl methods in the console, run:</p>
<p>At least in my case, I would get a <a href="">factory not registered</a> error when I didn&#8217;t do that.</p>
<h2>Better Errors</h2>
<p>Not exactly related to testing, but <a href="">this gem</a> has been a life changer! When there is an error, the standard error page gets replaced by a much better and useful error page, which has a full trace, the local and instance variables that are set at the time, and a terminal that you can use to inspect and make changes to the variables in the models and controllers in real time (or code whatever you want). I know I already mentioned it in another post, but I can&#8217;t stress enough how cool this gem is!</p>
<p>In the next posts, I&#8217;ll give an overview of how I test the models and controllers, which I only started testing this week!</p>
<title type="html"><![CDATA[Rails tip #2 - less Rspec in more color]]></title>
<link href=""/>
<content type="html"><![CDATA[<p>I usually have Guard running in the background with Spork, but when I&#8217;m writing tests I like to do it the old fashioned way and be able to do rspec some_test | less. The problem with that approach is that we get a monochrome version of our tests. In order to redirect the color in the pipe, edit spec_helper and add the following:</p>
<pre><code> # spec/spec_helper.rb
Rspec.configure do |config|
config.tty = true
config.color_enabled = true
# ... whatever configurations you might have here...
<p>And enjoy a more colorful life ;-)</p>
<title type="html"><![CDATA[Rails tip #1 - environment variables]]></title>
<link href=""/>
<content type="html"><![CDATA[<p>This isn&#8217;t exactly my tip (well, since this is a learning / work in progress blog, none of them are), but when an article is already as comprehensive as this one, there&#8217;s nothing left to say except:</p>
<p><a href="">Here&#8217;s a very nice article on the options for setting environment variables in Rails</a></p>
<p>Why would you want to set environment variables in rails? There are some examples in the article, but for instance, setting passwords to access services. They should not be in any file (nor tracked in git), so that other people cannot access them. One way is to set Unix environment variables, the other way &#8211; which actually sounds like a &#8220;cleaner way&#8221; of organizing things &#8211; is to set them from Rails itself by taking the values from a yaml file.</p>
<title type="html"><![CDATA[Controlling a Rapberry Pi via VNC without keyboard]]></title>
<link href=""/>
<content type="html"><![CDATA[<p>I don&#8217;t have a USB keyboard, so my only option for controlling my Raspberry Pi is via SSH. That turned out to work quite well, since the customized image of Debian provided by Raspberry Pi (<a href="">2012-10-28-wheezy-raspbian.img</a>) already has ssh enabled by default. It suffices to discover the IP address that was assigned by the router, and connect :-)</p>
<p>(By the way, for those with Mac OS, the command line instructions <a href="">here</a>) are very nice and comprehensive.</p>
<p>So now on to the cool stuff&#8230; Initally I had installed TightVNC, but could not connect to the same session as being displayed via HDMI (which is console 0). I don&#8217;t know whether that&#8217;s possible with TightVNC, but I sort of got tired of trying. So <a href="">here are the steps to make it work</a>, which basically consist of installing x11vnc instead of TightVNC and having your desktop manager autostart it:</p>
sudo apt-get install x11vnc
11vnc -storepasswd
# to create the autostart entry, do:
cd .config
mkdir autostart
cd autostart
nano x11vnc.desktop
# paste following text:
[Desktop Entry]
Exec=x11vnc -forever -usepw -display :0 -ultrafilexfer
<p>&#8230; aaand it works :-) For more details <a href="">check the original link</a>.</p>
<title type="html"><![CDATA[Installable Web Apps]]></title>
<link href=""/>
<content type="html"><![CDATA[<p><a href="">Very interesting remark from MIT Technology Review</a> on how installable web apps will be the next tech background. To be honest&#8230; Hope not. This should be the next big thing, especially for developers, who can make their apps ubiquitous with a little extra effort&#8230; It will be interesting to see Apple&#8217;s reaction, though.</p>
<title type="html"><![CDATA[Rack protection - another cool gem]]></title>
<link href=""/>
<content type="html"><![CDATA[<p>This one I haven&#8217;t evaluated yet, but looks promising: <a href="">Rack Protection</a>, to prevent typical web attacks such as <a href="">cross site request forgery</a>, <a href="">cross site scripting</a>, <a href="">clickjacking</a>, directory traversal, <a href="">session hijacking</a> and IP spoofing.</p>
<title type="html"><![CDATA[Grooveshark media center with raspberry pi]]></title>
<link href=""/>
<content type="html"><![CDATA[<p>&#8230; and better yet: in the command line. No more messing around with slooow browsers in the Pi.</p>
<p>First, install mplayer if you don&#8217;t already have it:</p>
<pre><code> sudo apt-get install mplayer </code></pre>
<p>Then, open ~/.mplayer/config and <a href="">pimp your player</a> by adding the following settings:</p>
<pre><code> cache=8192
cache-min=4 </code></pre>
<p>Next, clone the <a href="">gsclient plugin</a> by <a href="">drakedevel</a>:</p>
git clone</code></pre>
<p>And now&#8230; RoCk and RoLL :-)</p>
<title type="html"><![CDATA[Better errors gem]]></title>
<link href=""/>
<content type="html"><![CDATA[<p>Yes, today I am in &#8220;documentation&#8221; mode, which means &#8220;bloggation&#8221; mode :-)</p>
<p>Just installed this gem &#8211; <a href="">Better errors</a>. It is a gem meant to help you out in development mode that replaces the standard Rails error page with a wayyyy better and more useful error page, featuring full stack trace, source code inspection for all stack frames, local and instance variable inspection, etc.</p>
<p>I&#8217;m going to try it out today, but definitely beats the time spent looking at development logs (even if they are as clean and contained as mine after doing the changes described in <a href="">this post</a>).</p>
<title type="html"><![CDATA[Adding automatic code reloading to rails console]]></title>
<link href=""/>
<content type="html"><![CDATA[<p>So I have no idea why the rails console in development mode doesn&#8217;t already do this, since the whole idea of the console in development mode is for us to mess around and test random stuff in development mode, right? Anyway, I got inspired and helped by <a href="">Jkfill</a> with his post on <a href="">Automatic Code Reloading in Rails Console</a>, so here goes, more for my records than yours since you should follow the instructions in his post. I am using Rails 3.2.9, so I had to copy the code from the <a href="">reload! method in the rails console code</a>, which is not exactly the best practice, but it&#8217;s just for dev mode and it works :-)</p>
<pre><code> #this should be in config/initializers/irb_reloading.rb
if defined?(IRB::Context) && !defined?(Rails::Server) && Rails.env.development?
class IRB::Context
def evaluate_with_reloading(line, line_no)
evaluate_without_reloading(line, line_no)
alias_method_chain :evaluate, :reloading
puts "=> IRB code reloading enabled"
<p>In the meanwhile, shortly browsed the code at <a href="">the Rails gihub project</a> and it&#8217;s really a nice way to learn more about rails&#8230; must do it more often.</p>
<title type="html"><![CDATA[Silencing nagging logs in rails]]></title>
<link href=""/>
<content type="html"><![CDATA[<p>Once upon a time, I was trying to do some meaningful debugging in rails with</p>
<pre><code> logger.debug </code></pre>
<p>and I was being particularly nagged by these annoying messages from Webrick:</p>
<pre><code> WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true [closed]
<p>The way to solve this? I am using &#8216;thin&#8217; instead of Webrick as a development server (yeah, I know that there are <a href="">patches to solve this in Webrick</a> but I decided to go a bit aggressive instead).</p>
<p>And another thing that nagged me were the asset pipeline messages with the gets for the css, js, images, etc. So one way to shut Sprockets up is given <a href="">here</a>, and just for the record:</p>
<pre><code> # place this code in config/initializers/quiet_assets.rb
if Rails.env.development?
Rails.application.assets.logger ='/dev/null')
Rails::Rack::Logger.class_eval do
def call_with_quiet_assets(env)
previous_level = Rails.logger.level
Rails.logger.level = Logger::ERROR if env['PATH_INFO'] =~ %r{^/assets/}
Rails.logger.level = previous_level
alias_method_chain :call, :quiet_assets
<p>Ahhhhh now I can actually see <strong>meaningful</strong> logs in my development log. <em>whew!</em></p>
<title type="html"><![CDATA[New Octopress blog...]]></title>
<link href=""/>
<content type="html"><![CDATA[<p>I created this blog specifically to jot down notes of interesting / useful / meaningful stuff I find during my adventures with development, which I hope are useful to others.</p>
<p>And to start with, here is a link to a post that really helped me out <a href="">Octopress: Setting up a Blog and Contributing to an Existing One</a>. It wasn&#8217;t really clear to me from Octopress instructions that I had to run rake generate and rake deploy; I was just trying to make the git commits and push by hand (and becoming really frustrated by it). So, just for my own reference:</p>
<pre><code> # do stuff in your blog, like rake new_post
# then generate the blog
rake generate
# then save the source branch and push it to Github
git add .
git commit -m "whatever message"
git push origin source
# and now, this is where the magic happens! this takes care
# of pushing everything automagically onto the master branch!
rake deploy
<p>(depending on your system, you might need to run bundle exec followed by the rake commands).</p>
<p>Oh, and also&#8230; to delete a post, delete it only from <pre><code>blog-root/source/_posts/post-title</code></pre> and then run the commands above again. The rake tasks from Octopress take care of everything.</p>
<p>This cool minimalistic theme is from <a href=""></a>, and it&#8217;s called <a href="">Slash</a>.</p>
<p>So far I&#8217;m really enjoying Octopress &#8211; I chose it because I want to be able to do everything from the shell, and never leave my dev environment and my beloved ST2/emacs combination.</p>
Jump to Line
Something went wrong with that request. Please try again.