factory_girl snippets

How to install cucumber, webrat, rspec, shoulda, factory_girl

Tagged webrat, factory_girl, shoulda, cucumber  Languages ruby

In config/environments/test.rb:

config.gem 'cucumber', :lib => false
config.gem 'webrat', :lib => false
config.gem 'rspec', :lib => false
config.gem 'rspec-rails', :lib => false
config.gem 'thoughtbot-shoulda',
           :lib    => false,
           :source => 'http://gems.github.com'
config.gem 'thoughtbot-factory_girl',
           :lib    => false,
           :source => "http://gems.github.com"

Then from the command line:

rake gems:install RAILS_ENV=test

Then put the following in features/env.rb:

require 'factory_girl'
require 'spec/factories' 
require 'shoulda'

Shoulda+rr+factory_girl+fakeweb example

Tagged fakeweb, shoulda, factory_girl, rr  Languages ruby
require 'test_helper'

class FeedsControllerTest < ActionController::TestCase
  def stub_feed_url(url)
    body = feed_for_url(url)
    FakeWeb.register_uri(:get, "#{url}", :body => body)
  end

  def feed_for_url(url)
    uri = URI.parse(url)
    File.read("test/fixtures#{uri.path}")
  end

  context "on POST /subscribe" do
    context "with an existing feed" do
      setup do
        url = "http://dummy/valid-feeds/svd.se.xml"
        @user = Factory(:user)
        @feed = Feed.create!(:url => url)

        stub(@controller).current_user { @user }

        stub_feed_url(@feed.url)

        post :subscribe, :url => @feed.url
      end

      should_assign_to :subscription
      should_respond_with :success
      should_render_template :subscribe

      should "create a subscription" do
        assert_equal @user.subscriptions.size, 1
      end
    end
  end
end

test_helper.rb

.
.
require "rr"

include RR::Adapters::RRMethods

class ActiveSupport::TestCase
.
.