I'm at a standstill on this.
Everything in the tutorial went smoothly so far, but when I add this piece of code to the file / spec / requests / users _spec.rb, everything starts to go south:
describe "success" do it "should make a new user" do lambda do visit signup_path fill_in "Name", :with => "Example User" fill_in "Email", :with => "ryan@example.com" fill_in "Password", :with => "foobar" fill_in "Confirmation", :with => "foobar" click_button response.should have_selector("div.flash.success", :content => "Welcome") response.should render_template('users/show') end.should change(User, :count).by(1) end end
If I cleaned up the test database (rake db: test: prepare), all tests will pass. But if I run the tests again, they fail because the test database does not clear the record added by the code above.
I searched a bit in googled, and most of what I found pointed to either the config.use_transactional_fixtures parameter or the problem with embedding in the code.
I am pretty sure that none of them are suitable for me. Here is my spec_helper.rb file:
require 'rubygems' require 'spork' Spork.prefork do ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} RSpec.configure do |config| config.mock_with :rspec config.fixture_path = "#{::Rails.root}/spec/fixtures" config.use_transactional_fixtures = true
and here is my users_spec.rb:
describe "Users" do describe "signup" do describe "failure" do it "should not make a new user" do lambda do visit signup_path fill_in "Name", :with => "" fill_in "Email", :with => "" fill_in "Password", :with => "" fill_in "Confirmation", :with => "" click_button response.should render_template('users/new') response.should have_selector("div#error_explanation") end.should_not change(User, :count) end end describe "success" do it "should make a new user" do lambda do visit signup_path fill_in "Name", :with => "Example User" fill_in "Email", :with => "ryan@example.com" fill_in "Password", :with => "foobar" fill_in "Confirmation", :with => "foobar" click_button response.should have_selector("div.flash.success", :content => "Welcome") response.should render_template('users/show') end.should change(User, :count).by(1) end end end end
Any ideas? Thanks.
With mpapis answer, I was able to get this to work. Here is my updated spec / requests / user_spec.rb file:
require 'spec_helper' require 'database_cleaner' DatabaseCleaner.strategy = :truncation describe "Users" do describe "signup" do describe "failure" do it "should not make a new user" do lambda do visit signup_path fill_in "Name", :with => "" fill_in "Email", :with => "" fill_in "Password", :with => "" fill_in "Confirmation", :with => "" click_button response.should render_template('users/new') response.should have_selector("div#error_explanation") end.should_not change(User, :count) end end describe "success" do it "should make a new user" do lambda do visit signup_path fill_in "Name", :with => "Example User" fill_in "Email", :with => "ryan@example.com" fill_in "Password", :with => "foobar" fill_in "Confirmation", :with => "foobar" click_button response.should have_selector("div.flash.success", :content => "Welcome") response.should render_template('users/show') end.should change(User, :count).by(1) DatabaseCleaner.clean end end end end