Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby Rspec测试中未定义的局部变量或方法?_Ruby_Rspec - Fatal编程技术网

Ruby Rspec测试中未定义的局部变量或方法?

Ruby Rspec测试中未定义的局部变量或方法?,ruby,rspec,Ruby,Rspec,这是我的班级 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)

这是我的班级

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
我一直在变老

在中的“块(3级)”中:未定义的局部变量或方法“hero” RSpec::ExampleGroups::Hero::DefAttributes::AttackActions:类(NameError)


我不知道为什么。这是我写Rspec测试的第一天,所以请友好点…

您在上次测试中有一个输入错误,您忘记了单词
do

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

添加后所有内容都将通过。

您没有将块传递给
it
方法(您在末尾同时缺少
do
end

正确的代码应如下所示:

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