« back to discussion

Do you TDD your views?

I've recently have been running through Michael Hartl's 'Rails Tutorial' screencasts. One thing that seemed like a huge drag was testing the views. While capybara makes this less painless than using Jasmine... testing the dom seems really really tedious and time consuming.

This leads my to question... how often do you guys test your views in a Rails app? If so how much do you test?

over 3 years ago, by SkinnyGeek1010

10 Replies

pineapple

I test with poltergeist and capybara. Poltergeist is amazing because it uses PhantomJS (headless browser), and is able to execute JS. This means you don't have to sit there watching it execute your tests.

screencast on it

There are some special subtleties I do though, particularly with my test setup. It took me awhile to get it how I want it, so they all work together smoothly, but theres on particular issue with database transactions to watch out for (watch the screencast and you'll see the problem).

The solution is here. See point #3.

So I put that code in share_db_connection.rb in spec/support. Or this, if you want to copy-pasta

class ActiveRecord::Base
  mattr_accessor :shared_connection
  @@shared_connection = nil

  def self.connection
    @@shared_connection || retrieve_connection
  end
end

ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection

Here is my spec_helper.rb

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rails'
require 'capybara/rspec'
require 'capybara/poltergeist'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

Capybara.javascript_driver = :poltergeist

RSpec.configure do |config|
  config.include ShowMeTheCookies, :type => :feature
  config.include Sorcery::TestHelpers::Rails
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_base_class_for_anonymous_controllers = false
  config.order = "random"
end

and my gems

group :development do
  gem 'guard'
  gem 'guard-rspec'
  gem 'rb-fsevent'
  gem 'better_errors'
  gem 'binding_of_caller'
  gem 'rails-erd'
  gem 'wirb'
  gem 'wirble'
  gem 'thin'
end

group :development, :test do 
  gem 'zeus'
  gem 'rspec-rails', '~> 2.0'
  gem 'factory_girl_rails'
  gem 'show_me_the_cookies'
  gem 'pry'
  gem 'faker'
end

group :test do
  gem 'capybara'
  gem 'poltergeist'
end

I know that's not particularly what you were looking for as far as WHAT to test, but I can say I spent hours going through all this shit to get it how I wanted, and this was by far the best setup for my needs. Mr. Hartl's tutorials are the absolute bomb, but he gives terrible suggestions to use Spork which is old and busted. I use Zeus+Guard+Guard-Rspec which is 1000x better imo.

But yes, this makes it pretty damn painless to test views.

Oh.. one other thing to save you a ton of headache, you must rename your folder features instead of ... request I think. Or you will get errors.

pineapple, over 3 years ago

pineapple

I would definitely love to replace Factory Girl though... haven't found a good replacement yet.

pineapple, over 3 years ago

pineapple

I also am not a testing mega-guru, but I feel if my models are tested extremely well, I'm not as worried much about the rest. I only use the views for testing things like custom authentication and having the cookie persist (the 'remember me' functionality, etc). And other things that involve cookies or sessions.

pineapple, over 3 years ago

pineapple

I also test anything that is extremely important JS wise, where the functionality would break otherwise. But yeah, the short answer is "not much". :S

pineapple, over 3 years ago

SkinnyGeek1010

Oh wow.... so much good stuff here! Thanks!!

I need to try and get Guard running again... I think I had some kind of problems with rvm previously. Zeus looks reallllly cool too.

SkinnyGeek1010, over 3 years ago

pineapple

If you're using rvm, in the app root, make a file called .ruby-version and inside it put ruby-2.0.0 or whatever exact version you're using. that should also save some headache if you have a lot of ruby versions.

pineapple, over 3 years ago

SkinnyGeek1010
posted by pineapple on Thu, Jun 20 at 11:38PM

If you're using rvm, in the app root, make a file called .ruby-version and inside it put ruby-2.0.0 or whatever exact version you're using. that should also save some headache if you have a lot of ruby versions.

Nice, i'll do that tonight! Hopefully that fixes some errors with Vim plugins not using the correct ruby version too.

SkinnyGeek1010, over 3 years ago

pineapple
posted by SkinnyGeek1010 on Fri, Jun 21 at 07:05PM
posted by pineapple on Thu, Jun 20 at 11:38PM

If you're using rvm, in the app root, make a file called .ruby-version and inside it put ruby-2.0.0 or whatever exact version you're using. that should also save some headache if you have a lot of ruby versions.

Nice, i'll do that tonight! Hopefully that fixes some errors with Vim plugins not using the correct ruby version too.

Ahhh...... that would be a different issue entirely. Read the second-half of the readme on my dotfiles repo. I mostly talk about python, but the same applies for ruby.

pineapple, over 3 years ago

pineapple

This may be a tiny bit off; I'm not a python master, nor an expert in how vim compiles, but it's right enough where it should work for you.

What it boils down to is.. whatever version of ruby / python you have active in terminal (by running ruby -v for instance) at the time of installing macvim, thats the version it will compile with.

For me... having ruby 2.0 (installed with RVM), and python 2.7.5 (installed with brew) active at the time of brewing macvim, it DID use ruby properly.. but it did not use the proper Python. I'm not really sure why.. thats why I had to take those extra steps in my readme.

pineapple, over 3 years ago

pineapple

otool -L /usr/local/Cellar/macvim/7.3-66/MacVim.app/Contents/MacOS/Vim | grep python

and

otool -L /usr/local/Cellar/macvim/7.3-66/MacVim.app/Contents/MacOS/Vim | grep ruby

Should return the proper versions. It seems that my ruby has reset itself back to system ruby, yet the problems I faced when it was system ruby are no longer present (rspec not running from vim command line)... so yeah, i have no idea there.

You can definitely link ruby just as you can python though if you are facing problems.

pineapple, over 3 years ago


Login or to comment.

« back to discussion

Tutorials are any resources you learn from.

Examples: an intro to html5 screencast, a pdf about git, photoshop effects tutorials, meta-programming in ruby, lambda calculus, higher-order fixed-point combinators.

Tools are websites, apps or services used -on- your project (indirectly), to aid the process.

Examples: A color scheme generator, email marketing software, usability heat maps, css3 code generators, a downloadable png compressor.

Assets are downloadable files used -in- your projects, usually as code, textures, or images.

Examples: a jquery sticky menu, photoshop brushes, background textures, mvc frameworks, twitter bootstrap, 960 grid system.