在比较长文本(使用 \t 和 \n)时,如何使自动测试和 RSpec 格式良好?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现的最好的解决方案是这样的:
在我的规范中我使用
The best solution I found was this:
And in my spec I use
看看如何自定义期望匹配器 。您可以完全控制失败和负面失败消息。
Take a look at how to do Custom Expectation Matchers. You have full control over the failure and negative failure messages with those.