【问题标题】:TestFirst.org Dictionary Lesson Keyword Example RubyTestFirst.org 字典课程关键字示例 Ruby
【发布时间】:2015-01-04 01:08:53
【问题描述】:

我正在学习 TestFirst.org 的 Ruby 教程。我目前正在上“字典”课。我被困在关键字示例上。这是 Ruby 文档以及 RSpec 文件。

我感到困惑的原因是我不确定示例是否正确。如果按照 RSpec 文件,“可以检查给定关键字是否存在”之前的示例使用 add 方法将“鱼”添加到字典中。然后,在给我一个错误的例子中,包括?方法(我需要实现)应该返回 false。

它应该返回 false 吗?我们不是在上面的例子中添加了“鱼”吗?还是我错过了什么?

这是我的 Ruby 程序:

class Dictionary
    def initialize
        @entries = {}
    end

    def entries
        @entries
    end

    def keywords
        @entries.keys
    end

    def add(pairings)
        if pairings.is_a?(String)
            @entries[pairings] = nil 
        else
            pairings.each do |word, definition|
                @entries[word] = definition
            end
        end
    end

    def include?(keyword)
        @entries.keys.include?(keyword)
    end
end

以下是 RSpec 文件: # # 话题 # # * 哈希 # * 大批 # * 实例变量 # * 常用表达 #

require 'dictionary'

describe Dictionary do
  before do
    @d = Dictionary.new
  end

  it 'is empty when created' do
    @d.entries.should == {}
  end

  it 'can add whole entries with keyword and definition' do
    @d.add('fish' => 'aquatic animal')
    @d.entries.should == {'fish' => 'aquatic animal'}
    @d.keywords.should == ['fish']
  end

  it 'add keywords (without definition)' do
    @d.add('fish')
    @d.entries.should == {'fish' => nil}
    @d.keywords.should == ['fish']
  end

  it 'can check whether a given keyword exists' do
    @d.include?('fish').should be_false
  end

  it "doesn't cheat when checking whether a given keyword exists" do
    @d.include?('fish').should be_false # if the method is empty, this test passes with nil returned
    @d.add('fish')
    @d.include?('fish').should be_true # confirms that it actually checks
    @d.include?('bird').should be_false # confirms not always returning true after add
  end

  it "doesn't include a prefix that wasn't added as a word in and of itself" do
    @d.add('fish')
    @d.include?('fi').should be_false
  end

  it "doesn't find a word in empty dictionary" do
    @d.find('fi').should be_empty # {}
  end

  it 'finds nothing if the prefix matches nothing' do
    @d.add('fiend')
    @d.add('great')
    @d.find('nothing').should be_empty
  end

  it "finds an entry" do
    @d.add('fish' => 'aquatic animal')
    @d.find('fish').should == {'fish' => 'aquatic animal'}
  end

  it 'finds multiple matches from a prefix and returns the entire entry (keyword + definition)' do
    @d.add('fish' => 'aquatic animal')
    @d.add('fiend' => 'wicked person')
    @d.add('great' => 'remarkable')
    @d.find('fi').should == {'fish' => 'aquatic animal', 'fiend' => 'wicked person'}
  end

  it 'lists keywords alphabetically' do
    @d.add('zebra' => 'African land animal with stripes')
    @d.add('fish' => 'aquatic animal')
    @d.add('apple' => 'fruit')
    @d.keywords.should == %w(apple fish zebra)
  end

  it 'can produce printable output like so: [keyword] "definition"' do
    @d.add('zebra' => 'African land animal with stripes')
    @d.add('fish' => 'aquatic animal')
    @d.add('apple' => 'fruit')
    @d.printable.should == %Q{[apple] "fruit"\n[fish] "aquatic animal"\n[zebra] "African land animal with stripes"}
  end
end

【问题讨论】:

标签: ruby dictionary rspec test-first


【解决方案1】:

测试是正确的。您缺少的是 before 的工作原理。

在 RSpec 中,before 不带参数的挂钩(或带有参数 :each)指定在每个测试运行之前要执行的步骤,而不是(正如人们可能认为的)在整个测试集之前运行的步骤。

换句话说,before 钩子在第一次测试之前运行,然后在第二次测试之前,然后在第三次测试之前,依此类推。

执行顺序如下:

  1. before
  2. test1
  3. before
  4. test2
  5. before
  6. test3

在您的情况下,虽然在it 'add keywords (without definition)' do 中确实将'fish' 添加到@d,但这不会影响下一个测试,因为before 挂钩在它们两者之间运行,并替换@ 987654335@ 带有一个新的空 Dictionary 对象。因此,在测试 #include? 时,您正在检查一个空的 Dictionary 是否包含 'fish',而事实并非如此。

如果您想在运行任何测试之前采取一些步骤,您可以将 before 块替换为:

before(:all) do
  @d = Dictionary.new
end

在这种情况下,你是对的;该测试将使用与之前所有测试相同的Dictionary,因此应该期待true

【讨论】:

  • 感谢您的帮助。我不知道以前在 RSpec 工作过。我遇到问题的原因是因为我在使用另一个版本编写测试时使用的是 RSpec 3.0。
  • @user245185 你说得对。我没有提到它,因为我不知道你使用的是什么版本,而且它对你的问题似乎并不重要,但是,在 RSpec 3.0 中,测试应该写成be_truthybe_falsey ,分别。
猜你喜欢
  • 2013-01-09
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 2018-10-20
  • 1970-01-01
  • 2013-10-06
  • 2019-02-23
相关资源
最近更新 更多