微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – 运行RSpec时Zeus抛出错误

我遇到了Zeus和RSpec的许多用户似乎遇到的情况.

假设我有以下规格:

# spec/views/messages/show.html.erb

require 'spec_helper'

describe "messages/show.html.erb" do
  it "displays the text attribute of the message" do
    render
    rendered.should contain("Hello World!")
  end
end

以及它测试的以下视图模板.

# app/views/messages/show.html.erb

Hello World!

我的pwd是rails app的根源.当我运行rspec时,我收到以下响应:

user@host $rspec
.

Finished in 0.06264 seconds
1 example,0 failures

Randomized with seed 9609

一切都很好看.但是,如果我使用zeus rspec spec运行测试(zeus rspec根本不起作用),我得到以下输出.

user@host $zeus rspec spec
.

Finished in 0.07292 seconds
1 example,0 failures

Randomized with seed 0

F

Failures:

  1) messages/show.html.erb displays the text attribute of the message
     Failure/Error: render
     NameError:
       undefined local variable or method `render' for <RSpec::Core::ExampleGroup::nested_2:0x936a0fc>
     # ./spec/views/show.html.erb_spec.rb:5:in `block (2 levels) in <top (required)>'
     # -e:1:in `<main>'

Finished in 0.00041 seconds
1 example,1 failure

Failed examples:

rspec ./spec/views/show.html.erb_spec.rb:4 # messages/show.html.erb displays the text attribute of the message

我在SO上看到了几个与此类似的问题,例如:

Zeus fails when testing with Rspec

zeus rspec fails include required files,but rspec alone does fine

Zeus + FactoryGirl::Syntax::Methods. undefined method `create’

常见的线程是该解决方案建议在spec_helper.rb中注释掉/删除以下行(如果它们存在).

require 'rspec/autorun'
require 'rspec/autotest'

我的问题是这些行没有出现在我的spec_helper.rb文件中或应用程序中的任何其他位置.

经过一番挖掘后,我发现违规行实际上是RSpec gem中的rspec可执行脚本.

我目前正在使用RSpec版本2.13.1,文件内容如下:

#!/usr/bin/env ruby

begin
  require 'rspec/autorun'
rescue LoadError
  $stderr.puts <<-EOS
#{'*'*50}
  Could not find 'rspec/autorun'

  This may happen if you're using rubygems as your package manager,but it is not
  being required through some mechanism before executing the rspec command.

  You may need to do one of the following in your shell:

    # for bash/zsh
    export RUBYOPT=rubygems

    # for csh,etc.
    set RUBYOPT=rubygems

  For background,please see http://gist.github.com/54177.
#{'*'*50}
  EOS
  exit(1)
end

从RSpec文档中可以看出,它是一种方便的方法,可以为您运行测试.奇怪的是,当我注释掉开始/救援宙斯的’rspec / autorun’部分仍然具有相同的行为(抛出错误),但rspec不再以传统方式运行(只是运行rspec命令).根据这个文档(https://www.relishapp.com/rspec/rspec-core/docs/command-line),你仍然可以运行它,只是稍微冗长的方式.

在任何情况下,这都暗示我要么zeus要么依赖于rspec exec脚本以外的文件,要么问题与spec_helper.rb require语句(rspec 2.13.1不相关)不太明显相关添加到配置).

有没有人遇到这种情况或走下同样的道路?

我对在rspec-rails / rspec-core或zeus repos上抛出一个问题犹豫不决,因为我完全不确定哪个库导致了哪些问题.

任何帮助都将非常感激.

解决方法

根据 this,您应该在执行视图规范时遵循specs文件命名约定.尝试重命名
spec/views/messages/show.html.erb

spec/views/messages/show.html.erb_spec.rb

它可能会有所帮助.

原文地址:https://www.jb51.cc/ruby/269590.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐