执行Rspec时未定义的方法错误

时间:2013-02-01 13:53:10

标签: ruby-on-rails ruby rspec

这是我的work_spec.rb文件的样子。 运行该代码时,我收到了 by_letter 未定义的方法错误。

require 'spec_helper'
require 'factory_girl_rails'

describe Work do
  it "has a valid factory" do
    FactoryGirl.create(:work).should be_valid
  end
  it "is invalid without a last_name" do
     FactoryGirl.build((:work), last_name: nil).should be_valid
  end

  it "returns a sorted array of result that match" do
    smith = FactoryGirl.create((:work), last_name: "smith")
    jones = FactoryGirl.create((:work), last_name: "jones")
    johnson = FactoryGirl.create((:work), last_name: "johnson")

    Work.by_letter("j").should == [johnson, jones]
  end
end


Error receved while running
     Work returns a sorted array of result that match
     Failure/Error: Work.by_letter("j").should == [johnson, jones]
     NoMethodError:
     undefined method `by_letter' for #<Class:0x0000000429a708>
     # ./spec/models/work_spec.rb:17:in `block (2 levels) in <top (required)>'

1 个答案:

答案 0 :(得分:0)

失败的原因是你没有在by_letter类中定义方法Work