Rspec描述NameError未初始化的常量范围

时间:2013-08-19 12:16:25

标签: ruby rspec

我正在获取NameError未初始化的常量,如下所示:

Failures:

  1) Direction turn right faced south rotates to west
     Failure/Error: expect(Move.right(Direction::South)).to eq(Direction::West)
     NameError:
       uninitialized constant Move
     # ./spec/unit_tests/direction_spec.rb:11:in `block (4 levels) in <top (required)>'

  2) Direction turn right faced west rotates to north
     Failure/Error: expect(Move.right(Direction::West)).to eq(Direction::North)
     NameError:
       uninitialized constant Move
     # ./spec/unit_tests/direction_spec.rb:16:in `block (4 levels) in <top (required)>'

  3) Direction turn right faced north rotates to east
     Failure/Error: expect(Move.right(Direction::North)).to eq(Direction::East)
     NameError:
       uninitialized constant Move
     # ./spec/unit_tests/direction_spec.rb:21:in `block (4 levels) in <top (required)>'

  4) Direction turn right faced east rotates to south
     Failure/Error: expect(Move.right(Direction::East)).to eq(Direction::South)
     NameError:
       uninitialized constant Move
     # ./spec/unit_tests/direction_spec.rb:26:in `block (4 levels) in <top (required)>'

Finished in 0.02345 seconds
14 examples, 4 failures

这是我的代码:

require_relative '../spec_helper'
require 'direction'

describe Direction do

    subject(:Move) { Direction::Move.new }

    describe 'turn right' do
        context 'faced south' do
            it 'rotates to west' do
                expect(Move.right(Direction::South)).to eq(Direction::West)
            end
        end
        context 'faced west' do
            it 'rotates to north' do
                expect(Move.right(Direction::West)).to eq(Direction::North)
            end
        end
        context 'faced north' do
            it 'rotates to east' do
                expect(Move.right(Direction::North)).to eq(Direction::East)
            end
        end
        context 'faced east' do
            it 'rotates to south' do
                expect(Move.right(Direction::East)).to eq(Direction::South)
            end
        end
    end
end

主题不起作用的原因是什么?我也试过了let,但这不起作用

2 个答案:

答案 0 :(得分:2)

您必须使用小写的主题名称

subject(:move) { Direction::Move.new }

答案 1 :(得分:0)

如果要将Move解释为方法调用,则需要添加接收器或参数列表。即你需要使用

expect(self.Move.right(Direction::South)).to eq(Direction::West)

expect(Move().right(Direction::South)).to eq(Direction::West)

否则它将被解释为取消引用常量。