views:

41

answers:

1

Hey, I'm using TDD with rails for the first time... interesting concepts. Definitely useful. That is, until I come to this. When I run my test I get:

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'

where the test is:

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

and the supporting code is:

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

So obviously this works, but what's with the quotation marks? Have I missed something head-smackingly obvious?

+6  A: 

Your problem is coming from the fact that you are using be instead of eql. be is expecting a class in the way you've set it up (documentation). Try writing your spec as

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

Documentation for eql

Also, note the difference between eql and the method directly below it in the documentation, equal.

theIV
I normally write `@u1.fullname.should == "#{@attr[:firstname]} #{@attr[:lastname]}"` which i find more readable.
nathanvda