需要将模块放入spec文件以测试未定义的方法

时间:2014-06-25 14:00:50

标签: ruby-on-rails rspec capybara

我在

下有一个模块

应用程序/模型/关切/ system_name.rb

这个模块看起来像这样,我需要用方法

进行测试
module SystemName
  extend ActiveSupport::Concern

  def title_to_system_name(title)
    title.downcase.gsub(' ', '_').gsub(/[^0-9a-z\_]/i, '')
  end
end

这是我的spec文件

require "spec_helper"
require "#{Rails.root}/app/models/concerns/system_name.rb"

describe DataSource do 

  before(:each) do 
    @data_source = FactoryGirl.build_stubbed(:data_source)
  end 

  it "returns the system name in downcase format" do 
    expect(@data_source.system_name).to eq(title_to_system_name(@data_source.title))
  end 
end 

运行spec文件时出现此错误

Failures:

  1) DataSource returns the system name in downcase format
     Failure/Error: expect(@data_source.system_name).to eq(title_to_system_name(@data_source.title))
     NoMethodError:
       undefined method `title_to_system_name' for #<RSpec::ExampleGroups::DataSource:0x0000010a97b908>
     # ./spec/models/data_source_spec.rb:19:in `block (2 levels) in <top (required)>'

我确定我正确地需要模块或者我是否正确地执行了该模块?

更新我添加了SystemName

  it "returns the system name in downcase format" do 
    expect(@data_source.system_name).to eq(SystemName.title_to_system_name(@data_source.title))
  end 

这是我的模特

class DataSource < ActiveRecord::Base
  include SystemName

  #cut off a bunch of stuff
end

0 个答案:

没有答案