views:

68

answers:

2

I have a test in RSpec which compares to long text strings. When the test fails, I get a message like this:

'jobs partial should render the correct format for jobs' FAILED
expected: "Job {\n\tName = \"name1-etc\"\n\tType = Backup\n\tMessages = Daemon\n\tPool = Default
\n\tSchedule = \"schedule1\"\n\tStorage = storage1\n\tClient = \"name1\"\n\tFileset = \"fileset1
\"\n\tMax Wait Time = 5m\n\tWrite Bootstrap = \"/var/lib/bacula/name1-etc.bsr\"\n}\n\n",
     got: "Job {\n\tName = \"name1-etc\"\n\tType = Backup\n\tMessages = Daemon\n\tPool = Default
\n\tSchedule = \"schedule1\"\n\tStorage = storage1\n\tClient = \"name1\"\n\tFileset = \"fileset1
\"\n\tMax Wait Time = 5m\n\tWrite Bootstrap = \"/var/lib/bacula/name1-etc.bsr\"\n}\n\n" (using =
=)

How do I do to make RSpec and autotest respond a nicely formated diff (if possible, coloring the differences between texts? Something like this

expected:
Job {
    Name = "name1-etc"
    Type = Backup
    Messages = Daemon
    Pool = Default
    Schedule = "schedule1"
    Storage = storage1
    Client = "name1"
    Fileset = "fileset1" <--diff
    Max Wait Time = 5m
    Write Bootstrap = "/var/lib/bacula/name1-etc.bsr"
}
got:
Job {
    Name = "name1-etc"
    Type = Backup
    Messages = Daemon
    Pool = Default
    Schedule = "schedule1"
    Storage = storage1
    Client = "name1"
    Fileset = "fileset2" <-- diff
    Max Wait Time = 5m
    Write Bootstrap = "/var/lib/bacula/name1-etc.bsr"
}
A: 

Take a look at how to do Custom Expectation Matchers. You have full control over the failure and negative failure messages with those.

Jeff Waltzer
+1  A: 

The best solution I found was this:

module CustomMatchers
    class HaveTheSameText
      def initialize(expected)
        @expected = expected
      end

      def matches?(actual)
        @actual = actual
        @actual == @expected
      end

      def failure_message
        `diff #{file_for @expected} #{file_for @actual}`
      end

      def negative_failure_message
        "don't apply"
      end

    private
      def file_for text
     exp = Tempfile.new("bk", "/tmp").open
     exp.write(text)
     exp.close
     exp.path
      end
    end

    def have_the_same_text_of(expected)
      HaveTheSameText.new(expected)
    end
end

And in my spec I use

actual.should have_the_same_text_of expected
Daniel Cukier
Windows users will need to put cygwin in their path.
Jeff Waltzer