为什么我的guard-rspec 测试失败? [英] Why is my guard-rspec test failing?
问题描述
我使用 本教程.我也在练习命名空间,如编程 Ruby 中所述,从第 240 页开始.我当前的项目目录位于 github 在 stack-overflow-post
分支中.我正在使用:
I set up my guard-rspec
environment using the techniques described in this tutorial. I'm also practicing namespacing as described in Programing Ruby starting on page 240. My current project directory is on github in the stack-overflow-post
branch.
I'm using:
❯ ruby -v
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin15.0]
❯ rbenv -v
rbenv 1.0.0
❯ rbenv versions
system
* 2.1.1 (set by /Users/max/Google Drive/devbootcamp/phase-0-ruby-bulletin-board/.ruby-version)
2.1.5
2.2.3
2.3.1
❯ bundle exec guard -v
Guard version 2.3.0
我正在 spec/thread_spec.rb
中运行测试:
I'm running a test in spec/thread_spec.rb
:
require 'spec_helper'
describe 'RubyBulletinBoard::Thread' do
let (:thread) { RubyBulletinBoard::Thread.new }
it { expect(thread).to be_kind_of(RubyBulletinBoard::Thread) }
# it 'has a valid uuid' do
# p thread.uuid
# expect(thread.uuid).to be_kind_of(String)
# end
# it 'has editable title' do
# default_title = thread.title
# thread.title = "#{default_title} something different"
# new_title = thread.title
# expect(default_title == new_title).to eq(false)
# end
end
但是它失败了:
01:50:07 - INFO - Running: ./spec/thread_spec.rb:5
Run options: include {:locations=>{"./spec/thread_spec.rb"=>[5]}}
RubyBulletinBoard::Thread
example at ./spec/thread_spec.rb:5 (FAILED - 1)
Failures:
1) RubyBulletinBoard::Thread
Failure/Error: let (:thread) { RubyBulletinBoard::Thread.new }
NameError:
uninitialized constant RubyBulletinBoard::Thread
# ./spec/thread_spec.rb:4:in `block (2 levels) in <top (required)>'
# ./spec/thread_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 0.00055 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/thread_spec.rb:5 # RubyBulletinBoard::Thread
[1] guard(main)>
但我不知道为什么因为 RubyBulletinBoard::Thread
类确实存在于 lib/ruby_bulletin_board/thread.rb
中:
But I have no idea why because the class RubyBulletinBoard::Thread
does exist in lib/ruby_bulletin_board/thread.rb
:
require_relative 'id'
module RubyBulletinBoard
class Thread
attr_reader :uuid
attr_accessor :title
def initialize
@uuid = IdCreator.create_id
@title = title
end
end
end
更奇怪的是,当我取消注释 lib/ruby_bulletin_board/runner.rb
中的第 2 行时,测试通过了:
Even weirder, when I uncomment line 2 in lib/ruby_bulletin_board/runner.rb
the test passes:
require_relative 'example'
# require_relative 'thread' # <--- THIS IS LINE 2
module RubyBulletinBoard
class Runner
attr_accessor :greeter, :thread
def initialize
@greeter = RSpecGreeter.new
# @thread = RubyBulletinBoard::Thread.new
end
def run
greeter.greet
# p @thread.class
end
end
end
rspec 输出:
01:53:57 - INFO - Running: ./spec/thread_spec.rb:5
Run options: include {:locations=>{"./spec/thread_spec.rb"=>[5]}}
RubyBulletinBoard::Thread
should be a kind of RubyBulletinBoard::Thread
Finished in 0.00114 seconds
1 example, 0 failures
[1] guard(main)>
在我看来,在 runner.rb
中包含要求 thread.rb
与我的测试无关.
It seems to me that including requiring thread.rb
in runner.rb
would have nothing to do with my tests.
- 为什么我在
spec/thread_spec.rb
中的原始测试失败了? - 为什么取消注释
lib/runner.rb
中的第 2 行会导致测试通过?
- Why is my original test in
spec/thread_spec.rb
failing? - Why does uncommenting line 2 in
lib/runner.rb
cause the test to pass?
感谢您的帮助:)
接受的答案帮助我开始工作,更多问题可以在 另一篇文章
The accepted answer helped me get things up and running, more questions can be found in another post
编辑 2:也刚刚有了一个重大发现:我一直在使用一个名为 thread.rb
的文件,有一个名为 Thread 的 ruby 类,所以当我一直在说 require 'thread'
在我的 thread_spec.rb
它一直给我错误:
Edit 2:
Also just made a big discovery: I've been using a file named thread.rb
, there is a ruby class named Thread, so when I've been saying require 'thread'
in my thread_spec.rb
it's been giving me errors:
[1] guard(main)>
17:57:09 - INFO - Running: spec/thread_spec.rb
/Users/max/Google Drive/devbootcamp/phase-0-ruby-bulletin-board/spec/thread_spec.rb:4:in `<top (required)>': uninitialized constant RubyBulletinBoard (NameError)
from /Users/max/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1361:in `load'
from /Users/max/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1361:in `block in load_spec_files'
from /Users/max/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1359:in `each'
from /Users/max/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1359:in `load_spec_files'
from /Users/max/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:106:in `setup'
from /Users/max/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:92:in `run'
from /Users/max/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:78:in `run'
from /Users/max/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:45:in `invoke'
from /Users/max/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/exe/rspec:4:in `<top (required)>'
from bin/rspec:17:in `load'
from bin/rspec:17:in `<main>'
17:57:10 - ERROR - Failed: "bin/rspec -f progress -r /Users/max/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/guard-rspec-4.7.2/lib/guard/rspec_formatter.rb -f Guard::RSpecFormatter --failure-exit-code 2 spec/thread_spec.rb" (exit code: 1)
通过重命名 thread.rb
-> rbb_thread.rb
然后我可以 require 'rbb_thread'
并且我将红色变为绿色:)
by renaming thread.rb
-> rbb_thread.rb
I can then require 'rbb_thread'
and I'm going red to green :)
编辑 3:thread.rb
错误的另一个解决方案是 require_relative ../lib/thread
,因此不需要重命名
Edit 3:
Another solution for the errors with thread.rb
is to require_relative ../lib/thread
and so no rename is required
推荐答案
这里有几点:
当你运行规范时,你就像一个应用程序一样运行 RSpec,它的配置"在 spec/spec_helper.rb 中.我会假设它是空的/不相关的.
When you're running specs, you're running RSpec like an application and it's "config" is in spec/spec_helper.rb. I'll assume it's empty/irrelevant.
如果您运行的是更新版本的 RSpec,您只需将 --require spec_helper
放在您的 .rspec
文件中,您就不会在您的文件中需要 require "spec_helper"
.
If you're running a more recent version of RSpec, you can just place --require spec_helper
in your .rspec
file and you won't need the require "spec_helper"
in your files.
最好在第一次在文件中使用 RSpec.describe
而不是普通的 describe
.
It's best to use RSpec.describe
instead of plain describe
the first time in a file.
您可以将 subject
块与 scribed_class
一起使用以提高可读性.它还允许您使用 it { is.expected_to
语法
You can use a subject
block for more readability along with described_class
. It also lets you use it { is.expected_to
syntax
describe
可以使用类名而不是字符串.这样一来,您是否需要该文件就一目了然了.如果你传递一个类,那么 subject
就变成隐式了.
describe
can take a class name instead of a string. This way it makes it obvious if you've required the file or not. If you pass a class, then subject
becomes implicit.
由于 RSpec 就像一个单独的应用程序,默认情况下它不包含您自己的库文件(新版本的 RSpec 确实将 lib
添加到 LOAD_PATH
).所以你的测试必须单独需要他们测试的任何文件,例如
Since RSpec is like a separate app, it doesn't include your own library files by default (newer versions of RSpec do add lib
to LOAD_PATH
). So your tests have to individually require any files they test, e.g.
require 'spec_helper'
describe 'RubyBulletinBoard::Thread' do
let (:thread) { RubyBulletinBoard::Thread.new }
it { expect(thread).to be_kind_of(RubyBulletinBoard::Thread) }
变成:
require 'ruby_bulletin_board/thread'
RSpec.describe RubyBulletinBoard::Thread do
it { is_expected.to be_kind_of(RubyBulletinBoard::Thread) }
如果上述 require
失败,则意味着您需要将 lib
放入您的加载路径中.如果是这种情况,请检查您的 spec/spec_helper.rb
.
If the above require
fails, it means you need to put lib
in your load path. Check your spec/spec_helper.rb
if that's the case.
如果你的 spec/spec_helper.rb
是空的(或者没有任何重要的东西),你可能想把它和 .rspec
一起删除并运行 rspec --init
为您生成一个好的启动模板.确保取消注释 =begin
和 =end
部分.
If your spec/spec_helper.rb
is empty (or doesn't have anything important), you may want remove it along with .rspec
and run rspec --init
to generate a good started template for you. Be sure you uncomment the =begin
and =end
sections there.
这篇关于为什么我的guard-rspec 测试失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!