-3

我不明白为什么程序测试失败并且找不到错误。也许其他人可以给我一个提示?这是程序代码:

class Fiction_Generator
  def location_adj
    location_adj = ["alternative-timeline", 
       " Tolkeinesque", 
       " Neo-noir", 
       " Alternate-history", 
       " Ancient", 
       " Post-apocalyptic", 
       " Dystopian", 
       " Metaphorical", 
       " Anachronistic", 
       " Leather-clad" , 
       " Coal-powered", 
       " Dragon-filled"]
    item_adj = location_adj[rand(location_adj.length)]
  end
  def location_noun
     location_noun = [
       " America",
       " Japan",
       " Soviet Russia",
       " Steampunk Britain",
       " Medieval Europe",
       " Egyptian empire",
       " Atlantis",
       " Terraformed Mars",
       " Antarctica",
       " One-way spaceflight",
       " Outer Rim world",
       " Set from Road Warrior"]
     item_noun = location_noun[rand(location_noun.length)]
  end
  def protagonist
     protagonist = [
       " Ferris Bueller wannabe",
       " student of metaphysics",
       " milquetoast office drone",
       " schlub with mild <abbr>OCD</abbr>",
       " farm boy with dreams",
       " techno-obsessed geek",
       " brooding loner",
       " wisecracking mercenary",
       " idealistic revolutionary",
       " journeyman inventor",
       " collector of oddities",
       " rocketman of the 24th century"]
     item_protagonist = protagonist[rand(protagonist.length)]
  end
  def discovery
     discovery = [
       " magic ring",
       " arcane prophecy",
       " dusty tome",
       " crazy old woman",
       " alien artifact",
       " enchanted sword",
       " otherworldly portal",
       " dream-inducing drug",
       " encrypted data feed",
       " time-traveling soldier",
       " exiled angel",
       " talking fish"]
     item_discovery = discovery[rand(discovery.length)]
  end
  def adversary
    adversary = [
       " a megalomaniacal clown",
       " a government conspiracy",
       " a profit-obsessed corporation",
       " a sneering witch",
       " supernatural monsters",
       " computer viruses made real",
       " murderous robots",
       " an army led by a sadist",
       " forces that encourage conformity",
       " a charismatic politician on the rise",
       " humanity's selfish nature",
       " his own insecurity vis-a-vis girls"]
     item_adversary = adversary[rand(adversary.length)]    
  end
  def assistant
     assistant = [
       " sarcastic female techno-geek",
       " tomboyish female librarian",
       " shape-shifting female assassin",
       " leather-clad female in shades and red leather jacket",
       " girl who's always loved him for himself",
       " bookish female scholar with mousy brown hair",
       " cherubic girl with pigtails and spunk",
       " female who inexplicably becomes attracted to the damaged protagonist for  unstated reasons"]
    item_assistant = assistant[rand(assistant.length)]
  end
  def inventory
    inventory = [
       " wacky pet",
       " electric chainsaw",
       " closet full of assault rifles and one bullet",
       " reference book",
       " meat cleaver",
       " facility with magic",
       " condescending tone",
       " discomfort in formal wear"]
    item_inventory = inventory[rand(inventory.length)]
  end
  def conflict
    conflict = [
       " a fistfight atop a cable car",
       " a daring rescue preceding a giant explosion",
       " a demonic sacrifice",
       " a philosophical argument punctuated by violence",
       " a false victory with the promise of future danger",
       " the invocation of a spell at the last possible moment",
       " eternal love professed without irony",
       " the land restored to health",
       " authorial preaching through the mouths of the characters",
       " convoluted nonsense that squanders the reader's goodwill",
       " wish-fulfillment solutions to real-world problems",
       " a cliffhanger for the sake of prompting a series"] 
    item_conflict = conflict[rand(conflict.length)]
  end
  def title_adj
    title_adj = [
       " Time",
       " Micro",
       " Aero",
       " Cosmo",
       " Reve",
       " Necro",
       " Cyber",
       " Astro",
       " Psycho",
       " Steam",
       " Meta",
       " Black"]
    item_title_adj = title_adj[rand(title_adj.length)]
  end
  def title_noun
    title_noun = [
       " punks",
       " mechs",
       " noiacs",
       " opolis",
       " nauts",
       " phages",
       " droids",
       " bots",
       " blades",
       " trons",
       " mancers",
       " Wars"] 
    item_title_noun = title_noun[rand(title_noun.length)]
  end
  def title
    title_adj + title_noun
  end
  def story
    "In a(n)" + location_adj + location_noun + " a young" + protagonist + " stumbles          
     across a(an)" + discovery + " which spurs him into conflict with" + adversary + "     
     with the     help of a" + assistant + " and her" + inventory + " culminating in" +
     conflict
  end
  def all
    puts "Your story is called: " + #{title}
    puts "And it goes like this:"
    puts #{story}
  end
end

fict_gen = Fiction_Generator.new

fict_gen.all

测试是:

require "#{File.dirname(__FILE__)}/Fiction_Generator.rb"

  describe Fiction_Generator do

    before(:all) do
      @fict_gen = Fiction_Generator.new

    end

    it "should load up location adjectives" do
      @fict_gen.location_adj.size.should == 12
    end

    it "should load up location adjectives" do
      @fict_gen.location_adj.should include("coal-powered")
    end

    it "should load up location nouns" do
      @fict_gen.location_noun.size.should == 12
    end

    it "should load up location nouns" do
      @fict_gen.location_noun.should include("Japan")
    end

    it "should load up protagonist" do
      @fict_gen.protagonist.size.should == 12
    end

    it "should load up protagonist" do
      @fict_gen.protagonist.should include("collector of oddities")
    end


    it "should load up discovery" do
      @fict_gen.discovery.size.should == 12
    end

    it "should load up discovery" do
      @fict_gen.discovery.should include("encrypted data feed")
    end

    it "should load up adversary" do
      @fict_gen.adversary.size.should == 12
    end

    it "should load up assistant" do
      @fict_gen.assistant.size.should == 8
    end

    it "should load up inventory" do
      @fict_gen.inventory.size.should == 8
    end

    it "should load up conflict" do
      @fict_gen.conflict.size.should == 12
    end

    it "should load up title adjectives" do
      @fict_gen.title_adj.size.should == 12
    end

    it "should load up title noun" do
      @fict_gen.title_noun.size.should == 12
    end

    it "should randomly generate a title for the story" do
      @fict_gen.title.should be_true
    end

    it "should randomly generate a story using an item from each array" do
      @fict_gen.story.should be_true
    end

    after(:all) do
      puts "your story is called #{@fict_gen.title} "
      puts "and it goes like this:"
      puts @fict_gen.story
    end 
end

很抱歉代码很长,但我真的需要这个帮助。

它给出以下错误消息:

1) Fiction_Generator should load up location adjectives
Failure/Error: @fict_gen.location_adj.size.should == 12
  expected: 12
       got: 8 (using ==)
# ./fiction_spec.rb:21:in `block (2 levels) in <top (required)>'

2) Fiction_Generator should load up location adjectives
Failure/Error: @fict_gen.location_adj.should include("coal-powered")
  expected " Dragon-filled" to include "coal-powered"
# ./fiction_spec.rb:25:in `block (2 levels) in <top (required)>'

3) Fiction_Generator should load up location nouns
Failure/Error: @fict_gen.location_noun.size.should == 12
  expected: 12
       got: 11 (using ==)
# ./fiction_spec.rb:29:in `block (2 levels) in <top (required)>'

4) Fiction_Generator should load up protagonist
Failure/Error: @fict_gen.protagonist.size.should == 12
   expected: 12
       got: 30 (using ==)
# ./fiction_spec.rb:38:in `block (2 levels) in <top (required)>'

5) Fiction_Generator should load up protagonist
Failure/Error: @fict_gen.protagonist.should include("collector of odditi

   expected " student of metaphysics" to include "collector of oddities"
# ./fiction_spec.rb:42:in `block (2 levels) in <top (required)>'

6) Fiction_Generator should load up discovery
Failure/Error: @fict_gen.discovery.size.should == 12
   expected: 12
        got: 13 (using ==)
# ./fiction_spec.rb:47:in `block (2 levels) in <top (required)>'

7) Fiction_Generator should load up discovery
Failure/Error: @fict_gen.discovery.should include("encrypted data feed")
   expected " crazy old woman" to include "encrypted data feed"
# ./fiction_spec.rb:51:in `block (2 levels) in <top (required)>'

8) Fiction_Generator should load up adversary
Failure/Error: @fict_gen.adversary.size.should == 12
   expected: 12
        got: 26 (using ==)
# ./fiction_spec.rb:55:in `block (2 levels) in <top (required)>'

9) Fiction_Generator should load up assistant
Failure/Error: @fict_gen.assistant.size.should == 8
   expected: 8
        got: 27 (using ==)
# ./fiction_spec.rb:59:in `block (2 levels) in <top (required)>'

10) Fiction_Generator should load up inventory
Failure/Error: @fict_gen.inventory.size.should == 8
   expected: 8
        got: 10 (using ==)
# ./fiction_spec.rb:63:in `block (2 levels) in <top (required)>'

11) Fiction_Generator should load up conflict
Failure/Error: @fict_gen.conflict.size.should == 12
   expected: 12
        got: 44 (using ==)
# ./fiction_spec.rb:67:in `block (2 levels) in <top (required)>'

12) Fiction_Generator should load up title adjectives
Failure/Error: @fict_gen.title_adj.size.should == 12
   expected: 12
        got: 6 (using ==)
# ./fiction_spec.rb:71:in `block (2 levels) in <top (required)>'

13) Fiction_Generator should load up title noun
Failure/Error: @fict_gen.title_noun.size.should == 12
   expected: 12
        got: 7 (using ==)
# ./fiction_spec.rb:74:in `block (2 levels) in <top (required)>'
4

1 回答 1

0

您在此处列出的所有错误都发生在您的初始化方法中。在每种情况下,错误都是相同的,“参数数量错误(0 代表 1)”。

这意味着你需要看看你是如何构造你的 Fiction_Generator 对象的。在这种情况下,您的初始化方法要求您传入一个 IO 对象。所以不仅仅是使用

 fict_gen = Fiction_Generator.new

您需要在新方法中传递 IO 对象

 fict_gen = Fiction_Generator.new(#IO object here)

编辑:

所以你从上面的代码中删除了构造函数方法,现在我不知道你的初始化方法是什么样的,所以我写的可能不再适用。但是错误仍然存​​在于您的初始化方法中,因此请集中精力。

于 2013-03-24T17:51:45.000 回答