2012-10-27 100 views
3

我查了一下,但是找不到一个好的解释,所以这里是我的问题。用rspec测试rails引擎助手

我正在写一个Rails引擎,只添加助手。

的结构是这样的(重要的部分):

my_engine/ 
    -- app/ 
    -- helpers/ 
     -- my_engine/ 
      -- my_engine_helper.rb 
    -- spec/ 
    -- dummy/ 
    -- spec_helper.rb 

spec_helper.rb如下:

# Configure Rails Envinronment 
ENV['RAILS_ENV'] = 'test' 
require File.expand_path('../dummy/config/environment.rb', __FILE__) 

require 'rspec/rails' 

ENGINE_RAILS_ROOT = File.join(File.dirname(__FILE__), '../') 

# Requires supporting ruby files with custom matchers and macros, etc, 
# in spec/support/ and its subdirectories. 
Dir[File.join(ENGINE_RAILS_ROOT, 'spec/support/**/*.rb')].each { |f| require f } 

RSpec.configure do |config| 
    config.use_transactional_fixtures = true 
end 

到目前为止好,但我不知道如何测试我的帮手。 我想知道什么是最好的方式:

  • 组织的测试文件
  • 测试虚拟应用程序可以使用助手
  • 测试助手自己

现在运行规格时出现以下错误:

undefined local variable or method `helper' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fcee8b0f4a0> 

用以下测试:

require 'spec_helper' 

module MyEngine 
    describe MyEngineHelper do 
    describe '#something' do 
     it "does something" do 
     helper.something.should be_true 
     end 
    end 
    end 
end 

谢谢!

+0

凡位于相对规范的spec目录? –

+0

另外,你可以只需要'require'spec_helper'。 –

+0

好吧,我更新了规范。我将spec定位在spec/my_engine/my_engine_helper_spec.rb中,但我不知道它是否正确。 – Happynoff

回答

2

将它移动到spec/helpers/my_engine_helper_spec.rbrspec-railsspec/helpers中加入了helper方法。