Skip to content

Rails-Usage page example leads to conceptually wrong behavior #7

@alanjds

Description

@alanjds

Hi.

I am trying to test a Rails app with rubydoctest. To run the tests and integrate it with my CI bot, I set this rake task:

desc "Run all doctests"
task :doctest do
  require './config/environment'
  require 'database_cleaner'
  begin
    desc "Run doctests"
    sh "rubydoctest #{File.dirname(__FILE__)}/doc/doctests/*.doctest" do |ok, res|
      raise "Failed doctest." if !ok
    end
  rescue LoadError
    puts 'Oops: Cannot load doctests!'
  end
end

and this test helper, derived from https://github.com/tablatom/rubydoctest/wiki/Rails-Usage

ENV['RAILS_ENV'] = 'test'

require './config/environment'
require 'database_cleaner'

DatabaseCleaner.strategy = :truncation, {:except => %w[schema_migrations]}

module RubyDocTest
  class Test
    alias :old_pass? :pass?

    def pass?
      DatabaseCleaner.start
      result = old_pass?
      DatabaseCleaner.clean
      result
    end
  end
end

but something is going wrong when trying to create an User and reloading it from the database. The following fails, but only on the second part:

doctest: Create a new user and save to the database
>> u0 = User.create! :email => 'alberto@example.com', :password => 'password',
>> u0.id
=> 1

doctest: Be sure that the user is still at the database
>> User.all.length == 1

Its because each "doctest:" sign starts a new test. However, the database is reset to initial state but not the context. I believe that the database should be reset only after all the tests of this "file". Can someone please point me some piece of code to look for creating a patch, if possible?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions