Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
alanjds opened this issue May 29, 2012 · 2 comments
Open

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

alanjds opened this issue May 29, 2012 · 2 comments

Comments

@alanjds
Copy link

alanjds commented May 29, 2012

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?

@alanjds
Copy link
Author

alanjds commented May 30, 2012

I found that the recipe is the problem. The class monkeypatched should be Runner instead of Test as in:

module RubyDocTest
  class Runner
    alias :old_pass? :pass?

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

It works for me. I updated the documentation wiki

@alanjds alanjds closed this as completed May 30, 2012
@alanjds alanjds reopened this May 30, 2012
@alanjds
Copy link
Author

alanjds commented May 30, 2012

I has wrong. This "Runner monkeypatch" have no efect. Domentation reverted and bug reopened. Forking and trying to fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant