2016-06-05 2 views
0

this tutorial에 설명 된 기술을 사용하여 내 guard-rspec 환경을 설정했습니다. 또한 240 페이지에서 시작하는 Programing Ruby에 설명 된대로 네임 스페이스를 연습하고 있습니다. 현재 프로젝트 디렉토리는 브랜치에 github입니다. 내가 사용하고 있습니다 : 그것은 실패한 것 그러나가드 - rspec 테스트가 실패하는 이유는 무엇입니까?

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 

을 :

❯ 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에서 테스트를 실행하는거야

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 :

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에 라인이 주석을 제거 할 때 테스트를 통과 :

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)> 

이 포함와는 아무 상관이없는 것 thread.rbrunner.rb에서 요구하는 나에게 보인다 내 검사들.

  1. spec/thread_spec.rb의 원래 테스트가 실패한 이유는 무엇입니까?
  2. lib/runner.rb의 2 행의 주석 처리를 제거하면 테스트가 통과합니까? 당신의 도움 :)에 대한

감사

편집 : 허용 대답이 날 것하고 실행을 얻을 도움이

이, 더 많은 질문이 another post

편집 2에서 찾을 수 있습니다 : 방금 ​​발견 한 바가 크다. 나는 thread.rb이라는 파일을 사용하고 있었는데, Thread라는 이름의 루비 클래스가있다. 그래서 내가 sayi를했을 때 NG 내 thread_spec.rb에서 require 'thread'이 나에게주고 있어요 오류 :

[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' 나는 녹색 빨간색거야 수 :

편집 3 : 또 다른 방법 thread.rb의 오류는 require_relative ../lib/thread입니다. 따라서 이름을 바꾸지 않아도됩니다.

답변

1

여기 몇 가지가 있습니다 :

  1. 사양을 실행 중일 때 RSpec은 응용 프로그램처럼 실행되며 "config"는 spec/spec_helper.rb에 있습니다. 나는 그것이 비어 있거나 부적절하다고 가정 할 것이다.

  2. 최신 버전의 RSpec을 실행중인 경우 .rspec 파일에 --require spec_helper을 넣으면 파일에 require "spec_helper"이 필요하지 않습니다.

  3. 파일에서 처음으로 describe 대신 RSpec.describe을 사용하는 것이 가장 좋습니다.

  4. described_class과 함께 더 읽기 쉽도록 subject 블록을 사용할 수 있습니다. 또한 it { is.expected_to 구문을 사용할 수 있습니다.

  5. describe은 문자열 대신 클래스 이름을 사용할 수 있습니다. 이렇게하면 파일을 필요로했는지 여부를 쉽게 알 수 있습니다. 클래스를 전달하면 subject이 암시 적이됩니다. RSpec에 별도의 응용 프로그램처럼이기 때문에

, 그것은 기본적으로 (RSpec에 새로운 버전이 LOAD_PATHlib를 추가하려면) 자신의 라이브러리 파일을 포함하지 않습니다. 따라서 테스트를 위해 파일을 개별적으로 테스트해야합니다.

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을 확인하십시오.

spec/spec_helper.rb이 비어 있거나 (중요하지 않은 경우) .rspec과 함께 제거하고 rspec --init을 실행하여 좋은 시작 템플릿을 생성 할 수 있습니다. 거기에 및 =end 섹션의 주석 처리를 제거하십시오.

+0

감사합니다. 편집을 참조하십시오. – mbigras