[rspec-devel] [ rspec-Bugs - Rails Plugin-6375 ] rails_spec does not isolate tests when using database
noreply at rubyforge.org
noreply at rubyforge.org
Sat Oct 28 16:58:59 EDT 2006
Bugs - Rails Plugin item #6375, was opened at 2006-10-28 17:23
You can respond by visiting:
http://rubyforge.org/tracker/?func=detail&atid=9465&aid=6375&group_id=797
Category: None
Group: None
Status: Open
Resolution: None
Priority: 3
Submitted By: Nobody (None)
Assigned to: Nobody (None)
Summary: rails_spec does not isolate tests when using database
Initial Comment:
Specs are not isolated between runs
I have model Employee with
validates_uniqueness_of :login
then in my EmployeeSpec (only 1 context, 1 spec) i execute:
employee = Employee.new(valid_employee_attributes)
employee.save.should.be true
I do NOT load fixtures.
When I run this spec for the FIRST time -- it is succeeds.
But when I run it once again (with spec, or with use of rails_spec_server)
it fails:
Spec::Expectations::ExpectationNotMetError in 'New Employee should have all attributes set'
"has already been taken" should be nil
Which means that there is already an Employee with such name.
Then I checked my database server and it showed (SELECT * FROM employees) existing records!!!.
I also fetched all employees within the spec before new record creation:
ee = Employee.find :all
ee.each do |x|
puts "#{x.id} #{x.login}"
end
which showed records from DB, but there should be no records -- as this is test DB. That means that this test (spec) was not isolated.
I used self.use_transactional_fixtures set to 'true' and 'false'
----------------------------------------------------------------------
Comment By: Michal Bakowski (dr_bonzo)
Date: 2006-10-28 22:58
Message:
Source of bug found.
I started building this spec from scratch:
---8<-------------------------------
Employee with
validates_uniqueness_of :login
(...)
context "New Employee" do
include EmployeeSpecHelper
def setup # important, I also used in in previous case, but not included it in code listing
puts "===================="
end
specify "should have all attributes set" do
employee = Employee.new(valid_employee_attributes)
employee.employee_group_id = 0
employee.save.should.be true
end
end
================================8<============
And I have finally located source of this bug. It was this simple 'setup' method (I needed it for debugging (separating debug messages) spec as it does not have 'breakpoint' feature :( ).
OMG, my mistake (RTFM):
it is not "def setup..." (T::U way) but "setup do..." (RS)
How to reproduce bug:
I clear the DB.
Run this spec (spec .../employee_spec.rb)
It passes.
There is 1 record in DB
Run it once more
I get the error:
---
Solution
I remove 'setup' method from spec
Run this spec (spec .../employee_spec.rb)
It passes.
DB is empty
Run it once more
And it is ok (spec passed, DB empty)
Thanks for motivation, as I returned to Test::Unit after that failure :)
Michal Bakowski
----------------------------------------------------------------------
Comment By: David Chelimsky (dchelimsky)
Date: 2006-10-28 19:49
Message:
Since you were playing with self.use_transactional_fixtures, you may have created the record when that was set to false. Can you please manually ensure that your database table is empty, set self.use_transactional_fixtures to true and run the spec twice and report back if this still happens?
Thanks,
David
----------------------------------------------------------------------
You can respond by visiting:
http://rubyforge.org/tracker/?func=detail&atid=9465&aid=6375&group_id=797
More information about the rspec-devel
mailing list