Topic: Test works in dev console, but not in test environment

Hello all, in my recent dive into TDD I am learning lots and lots.  I actually got the whole app passing the various tests yesterday... good feeling

However, after adding another test, I am running into a problem.  Specifically...

I have a model - document which has a method file_cleanr.  Looks like this:

class Document < ActiveRecord::Base

  def prepare_document_for_import
    # Copy the original document over to the to be cleaned document field (saving original)
    self.cleaned_document = original_document
    # Clean the file up of unwanted characters and tags
    self.cleaned_document = file_cleaner
  end

  def file_cleaner
    document = self.cleaned_document
    document = document.chars.gsub(/\n/u, ' ')
    document = document.chars.gsub(/\r/u, ' ')
    document = document.chars.gsub(/\s/u, ' ')
    document = document.chars.squeeze(" ")
    document = document.chars.gsub(/<p /u, "\n<p ")
    document = document.chars.gsub(/(\w&#8212;\w)/u, '&mdash;')
    self.cleaned_document = document
  end


And I have the following test to check if it is all working:

require File.dirname(__FILE__) + '/../test_helper'
class DocumentTest < Test::Unit::TestCase

  def test_should_ensure_cleaned_document_is_clean_of_unwanted_characters
    word_file = File.new("#{File.expand_path(RAILS_ROOT)}/test/fixtures/Test.htm")
    document = create(:cleaned_document => word_file.read)

    # Sanity check, make sure we are working on a valid file
    document.cleaned_document =~ /\r\n/m
    assert $&, "Test file should have double line feeds"
    document.cleaned_document =~ /(\w&#8212;\w)/u
    assert $&, "Test file should have an unicode U+2014 mdash"

    document.word_file_cleaner
    document.save!

    document.cleaned_document =~ /\r\n/u
    deny $&, "Text should not have any double line feeds"
    document.cleaned_document =~ /\r/u
    deny $&, "Text should not have any carriage returns"
    document.cleaned_document =~ /(\w&#8212;\w)/u
    deny $&, "Text should not have an unicode mdash"
  end


create in the test is a private test method that creates a template document.  deny is a wrapper for assert !.  Both of these work elsewhere.

The test fails at the first deny.  So it does assert that the regexps are in the cleaned_document attribute.

Further.  From the development console, I can do the following:

document = Document.find(4)
document.cleaned_document = document.original_document
document.prepare_document_for_import
document.cleaned_document

And I'll get a nicely cleaned file.

I thought that the document text was not being imported properly, so I tried putting #{document.cleaned_document} into the assert that was failing, it showed all the text from the file on the screen, with carriage returns and newlines, so this isn't the problem.

So, i am getting one result in the development console and another from the test environment.  Any ideas?

Regards

Mikel

Re: Test works in dev console, but not in test environment

I found the answer.

I was using $& as the assert / !assert.  This meant that if there was a nil value then the previous match would still be in the $& as it is a global variable.

Changing it to something like this, solved the problem.

    test = document.cleaned_document =~ /\r\n/u
    deny test, "Text should not have any double line feeds"
    test = nil

Off to fix more things smile

Regards

Mikel