为什么我得到这个(1)错误?

时间:2011-05-20 04:38:04

标签: ruby-on-rails rspec

我得到一个我不会期望的rspec错误,我最终以另一种方式检查。但是,我想知道为什么它不起作用。错误如下:

 Failure/Error: @user.should have(1).work_shift
 RuntimeError:
   expected work_shift to be a collection but it does not respond to #length or #size

规范是:

        it "should create the work shift" do
            post :start, :work_hours => 6
            flash[:error].should == nil
            @user.should have(1).work_shift
            response.should redirect_to labor_url                       
        end

为什么我会收到错误? (用户has_one work_shift,协会工作正常)

2 个答案:

答案 0 :(得分:1)

expected work_shift to be a collection

由于用户只有一个work_shift

@user.work_shift

没有给出数组。

RSpec消息说

but it does not respond to #length or #size

因此匹配器正在寻找类似于数组的对象。

答案 1 :(得分:1)

我想你可能想要:

@user.work_shift.should_not == nil

也可以写成:

@user.work_shift.should be_present