2014-11-23 1 views
0

여기 내 수업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) 
    end 

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

end 

입니다 그리고 이러한

I 블록 (3 단계)에서`에

을 받고 계속
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 

내 시험이다 '정의되지 않은 지역 변수 또는 메서드'영웅 '에 대해 RSpec :: ExampleGroups :: Hero :: DefAttributes :: AttackActions : 클래스 (NameError)

그리고 나는 이유를 모른다.

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

모든 것이 한 번에 추가 패스 : 이것은 당신이 단어 do을 잊고, 당신은 당신의 마지막 테스트에 오타가 RSpec에를 작성하는 것은 너무 좋은 주시기 바랍니다 테스트 첫 날 ...

답변

0

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 
관련 문제