Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在RSpec/Rails验收测试中,是否有任何方法将消息推送到RSpec输出并保持良好的缩进?_Ruby On Rails_Rspec - Fatal编程技术网

Ruby on rails 在RSpec/Rails验收测试中,是否有任何方法将消息推送到RSpec输出并保持良好的缩进?

Ruby on rails 在RSpec/Rails验收测试中,是否有任何方法将消息推送到RSpec输出并保持良好的缩进?,ruby-on-rails,rspec,Ruby On Rails,Rspec,我的验收测试(RSpec/Capybara)在一个it…do块下有一些很长的步骤序列,我想手动将每个步骤的一些附加文档添加到RSpec文档输出中 因此,RSpec输出(以文档格式)当前看起来像: FooController New user creates account and creates profile it passes 在漫长的步骤序列中,我想将一些附加信息推送到输出: FooController New user creates account and creat

我的验收测试(RSpec/Capybara)在一个
it…do
块下有一些很长的步骤序列,我想手动将每个步骤的一些附加文档添加到RSpec文档输出中

因此,RSpec输出(以文档格式)当前看起来像:

FooController
  New user creates account and creates profile
    it passes
在漫长的步骤序列中,我想将一些附加信息推送到输出:

FooController
  New user creates account and creates profile
    ... new user saw signup page!
    ... new user got email confirmation!
    ... confirmation link worked!
    ... new user saw empty profile!
    ... new user filled in profile
    it passes
在记录应用程序方面,这些额外的语句比一个带有单个“it passed”结果消息的大黑盒子要好


由于显然没有办法使用多个
it…do
块来构建验收测试的长序列步骤,因此我希望有一种简单的方法将额外的消息推送到RSpec输出流,理想情况下是将它们缩进并显示(红色/绿色)就好像它们是单独的一样。
it…做一些例子。

以下是我所做的。。。在我的规范中添加了一个nextstep(“message”)方法,该方法使用awesome_print gem将“message”输出到控制台,以便我可以对其进行着色,也可以输出到记录器

  def nextstep(txt)
    $step += 1
    @speclog.debug ''
    @speclog.debug ''
    @speclog.debug "#{$scene}, step: #{$step}: " + txt
    ap (' ' * ($indent * 2 - 1)) + "step: #{$step}: " + txt, {:color => {:string => :blueish}}
  end
虽然有点粗俗,但在运行rspec时,它确实为我们提供了非常好的描述性输出,例如,如果我们有

it "New user creates account and creates profile" do
   # some assertions
   nextstep "... new user saw signup page!"
   # some assertions
   nextstep " ... new user got email confirmation!"
   # some assertions
   nextstep " ... confirmation link worked!"
   # some assertions
   nextstep "... new user saw empty profile!"
   # some assertions
   nextstep "... new user filled in profile"
end
我们得到了问题中所示的更具描述性的规范输出(如果出现故障,我们将看到我们所处的步骤):


最后,我选择了定制,在
spec/support
文件夹的某个地方包含以下代码(以确保自动加载):

将产生以下输出(步骤字符串颜色为浅蓝色):


诚然,这是一个非常粗糙的解决方案,但只要多做一点工作,它可能会变得更好。

在shoulda中,您可以通过此解决方案实现它:但我感到震惊的是,在rspec中这是不可能的。这似乎是一个很好的解决方案,但我无法理解关键的
$indent
变量来自何处。哦-在我的规范中,我有一个$indent变量,可以控制缩进,这样子测试输出可以缩进得更深这是一个非常好的解决方案。。。如果您让rspec随机化测试顺序(例如,不使用defaultorder),那么“步骤”是否仍会按照规范中指定的顺序执行?好问题!实际上,我不知道,因为我通常不会随机化文档格式化程序的顺序。
   step 1: ... new user saw signup page!
   step 2: ... new user got email confirmation!
   step 3: ... confirmation link worked!
   step 4: ... new user saw empty profile!
   step 5: ... new user filled in profile"
require "rspec/core/formatters/documentation_formatter"

class RSpec::Core::ExampleGroup
  def step(msg)
    example.metadata[:step_messages] << msg if example.metadata[:step_messages]
    yield
  end
end

class RSpec::Core::Formatters::DocumentationFormatter
  def example_started(example)
    example.metadata[:step_messages] = []
  end

  def example_passed(example)
    output.puts passed_output(example)
    print_steps(example)
  end

  private
    def print_steps(example)
      example.metadata[:step_messages].each do |msg|
      output.puts detail_color("#{'  ' * (@group_level + 1)}#{msg}")
    end
  end
end
it "should show all and only appoved posts" do
  step "show all approved posts" do
    Post.all.approved.each do |post|
      should have_content(post.title)
    end
  end
  step "show only approved posts" do
    should have_selector(".post", count: Post.all.approved.count)
  end
end
should show all and only appoved posts
  show all approved posts
  show only approved posts