【问题标题】:Undefined local variable or method in Rspec test?Rspec测试中未定义的局部变量或方法?
【发布时间】:2014-11-23 23:25:13
【问题描述】:

这是我的课

class Hero
  attr_reader :strength, :health, :actions

  def initialize(attr = {})
    @strength = attr.fetch(:strength, 3)
    @health = attr.fetch(:health, 10)
    @actions = attr.fetch(:actions, {})

    @dicepool = attr.fetch(:dicepool) 
  end

  def attack(monster)
    @dicepool.skill_check(strength, monster.toughness)
  end

end

这些是我的测试

require 'spec_helper'
require_relative '../../lib/hero'

describe Hero do
  let(:dicepool) {double("dicepool")}

  describe "def attributes" do
    let(:hero){Hero.new dicepool: dicepool}

    it "has default strength equal to 3" do
      expect(hero.strength).to eq(3)
    end
    it "has default health equal to 10" do
      expect(hero.health).to eq(10)
    end

    it "can be initialized with custom strength" do
      hero = Hero.new strength: 3, dicepool: dicepool
      expect(hero.strength).to eq(3)
    end

    it "can be initialized with custom health" do
      hero = Hero.new health: 8, dicepool: dicepool
      expect(hero.health).to eq(8)
    end

    describe "attack actions" do
      let(:attack_action) {double("attack_action") }
      let(:hero) {Hero.new dicepool: double("dicepool"), actions: {attack: attack_action} }

      it "has attack action"
        expect(hero.actions[:attack]).to eq(attack_action)
      end
    end  


end

我一直在努力

in `block (3 levels) in ': 未定义的局部变量或方法 'hero' for RSpec::ExampleGroups::Hero::DefAttributes::AttackActions:Class (NameError)

我不知道为什么。这是我编写 Rspec 测试的第一天,所以请多多关照...

【问题讨论】:

    标签: ruby rspec


    【解决方案1】:

    你在上次测试中有错字,你忘记了do这个词:

      it "has attack action" do
        expect(hero.actions[:attack]).to eq(attack_action)
      end
    

    一旦添加,一切都会过去。

    【讨论】:

      【解决方案2】:

      您没有将块传递给it 方法(最后您缺少doend)。

       it "has attack action"
                              ^^^
      

      正确的代码应该是这样的:

      describe Hero do
        let(:dicepool) {double("dicepool")}
      
        describe "def attributes" do
          let(:hero){Hero.new dicepool: dicepool}
      
          it "has default strength equal to 3" do
            expect(hero.strength).to eq(3)
          end
          it "has default health equal to 10" do
            expect(hero.health).to eq(10)
          end
      
          it "can be initialized with custom strength" do
            hero = Hero.new strength: 3, dicepool: dicepool
            expect(hero.strength).to eq(3)
          end
      
          it "can be initialized with custom health" do
            hero = Hero.new health: 8, dicepool: dicepool
            expect(hero.health).to eq(8)
          end
      
          describe "attack actions" do
            let(:attack_action) {double("attack_action") }
            let(:hero) {Hero.new dicepool: double("dicepool"), actions: {attack: attack_action} }
      
            it "has attack action" do
              expect(hero.actions[:attack]).to eq(attack_action)
            end
          end
        end
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多