在比较长文本(使用 \t 和 \n)时,如何使自动测试和 RSpec 格式良好?

发布于 2024-08-07 02:32:35 字数 1450 浏览 2 评论 0原文

我在 RSpec 中有一个测试,它与长文本字符串进行比较。当测试失败时,我收到这样的消息:

'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 well-formated diff (if possible, coloring the Differences between text? 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"
}

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"
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哑剧 2024-08-14 02:32:35

我发现的最好的解决方案是这样的:

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

在我的规范中我使用

actual.should have_the_same_text_of expected

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
晚风撩人 2024-08-14 02:32:35

看看如何自定义期望匹配器 。您可以完全控制失败和负面失败消息。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文