2010-09-10 30 views
0

嘿,我第一次使用TDD和rails ......有趣的概念。絕對有用。也就是說,直到我來到這裏。當我運行我的測試,我得到:「Joe Smith」與rspec中的Joe Smith不一樣嗎?

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/[email protected]/gems/rspec-core-2.0.0.beta.18/lib/rspec/monkey/spork/test_framework/rspec.rb:4:in `run_tests' 
    # /home/brian/.rvm/gems/[email protected]/gems/spork-0.8.4/lib/spork/run_strategy/forking.rb:13:in `block in run' 
    # /home/brian/.rvm/gems/[email protected]/gems/spork-0.8.4/lib/spork/forker.rb:21:in `block in initialize' 
    # /home/brian/.rvm/gems/[email protected]/gems/spork-0.8.4/lib/spork/forker.rb:18:in `fork' 
    # /home/brian/.rvm/gems/[email protected]/gems/spork-0.8.4/lib/spork/forker.rb:18:in `initialize' 
    # /home/brian/.rvm/gems/[email protected]/gems/spork-0.8.4/lib/spork/run_strategy/forking.rb:9:in `new' 
    # /home/brian/.rvm/gems/[email protected]/gems/spork-0.8.4/lib/spork/run_strategy/forking.rb:9:in `run' 
    # /home/brian/.rvm/gems/[email protected]/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 

所以,很顯然這工作,但什麼是與報價分數?我是否錯過了一些頭腦明顯的東西?

+0

你有沒有理由不使用'@ u1.firstname'和'@ u1.lastname'? – theIV 2010-09-10 19:49:56

回答

6

您的問題來自於您使用的是be而不是eqlbe正按照您設置的方式(documentation)期待課程。嘗試寫你的規格爲

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

Documentation for eql

另外,還要注意eql和文檔,equal在正下方的方法之間的差異。

+0

我通常寫'@u1.fullname.should ==「#{@ attr [:firstname]}#{@ attr [:lastname]}」'我發現更具可讀性。 – nathanvda 2010-09-10 22:14:59

相關問題