“乔史密斯”与史密斯的乔史密斯不一样?

时间:2010-09-10 19:45:50

标签: ruby-on-rails tdd rspec

嘿,我第一次使用带轨道的TDD ......有趣的概念。绝对有用。也就是说,直到我来到这里。当我进行测试时,我得到:

1) User should build the full name correctly
    Failure/Error: @u1.fullname.to_s.should be("#{@attr[:firstname]} #{@attr[:lastname]}")
    expected Joe Smith, got "Joe Smith"
    # ./spec/models/user_spec.rb:35:in `block (2 levels) in <top (required)>'
    # /home/brian/.rvm/gems/ruby-1.9.2-p0@seniorproject/gems/rspec-core-2.0.0.beta.18/lib/rspec/monkey/spork/test_framework/rspec.rb:4:in `run_tests'
    # /home/brian/.rvm/gems/ruby-1.9.2-p0@seniorproject/gems/spork-0.8.4/lib/spork/run_strategy/forking.rb:13:in `block in run'
    # /home/brian/.rvm/gems/ruby-1.9.2-p0@seniorproject/gems/spork-0.8.4/lib/spork/forker.rb:21:in `block in initialize'
    # /home/brian/.rvm/gems/ruby-1.9.2-p0@seniorproject/gems/spork-0.8.4/lib/spork/forker.rb:18:in `fork'
    # /home/brian/.rvm/gems/ruby-1.9.2-p0@seniorproject/gems/spork-0.8.4/lib/spork/forker.rb:18:in `initialize'
    # /home/brian/.rvm/gems/ruby-1.9.2-p0@seniorproject/gems/spork-0.8.4/lib/spork/run_strategy/forking.rb:9:in `new'
    # /home/brian/.rvm/gems/ruby-1.9.2-p0@seniorproject/gems/spork-0.8.4/lib/spork/run_strategy/forking.rb:9:in `run'
    # /home/brian/.rvm/gems/ruby-1.9.2-p0@seniorproject/gems/spork-0.8.4/lib/spork/server.rb:47:in `run'

测试的地方是:

it 'should build the full name correctly' do
  @u1.fullname.should be("#{@attr[:firstname]} #{@attr[:lastname]}")
end

,支持代码为:

def fullname
  "#{firstname} #{lastname}"
end

显然这很有效,但引号是什么?我错过了一些令人头疼的东西吗?

1 个答案:

答案 0 :(得分:6)

您的问题来自于您使用的是be而不是eqlbe期待按照您设置的方式进行课程(documentation)。尝试将您的规范编写为

@u1.fullname.should eql("#{@attr[:firstname]} #{@attr[:lastname]}")

Documentation for eql

另请注意文档eqlequal与其正下方法之间的区别。