How to write / run specifications for files other than model / view / controller

Using rails and rspec it is easy to get rspec to create the necessary files for me when I use the command rails generatewith models / views / controllers. But now I want to write specifications for the module that I wrote. The module is in /lib/my_module.rb, so I created the specification in/spec/lib/my_module_spec.rb

The problem that I am facing is that when I try to execute rspec spec/, the file is executed my_module_spec.rb, but the link to my module in lib/my_module.rbcannot be found. What is the right way to do this?

Just FYI in the file my_module_spec.rbis require 'spec_helper'already in it

require 'spec_helper'

describe "my_module" do
  it "first test"
    result = MyModule.some_method  # fails here because it can't find MyModule
  end
end
+5
3

, ,

require 'spec_helper'

#EDIT according to 
# http://stackoverflow.com/users/483040/jaydel
require "#{Rails.root}/lib/my_module.rb"

describe MyModule do

  let(:wrapper){
    class MyModuleWrapper
      include MyModule
    end
    MyModuleWrapper.new
  }

  it "#some_method" do
    wrapper.some_method.should == "something"
  end

end
+9

? ,

MyModule.some_method

, some_method

def self.some_method
  ...
end

, Jasper. , .

+1

config/application.rb :

config.autoload_paths += %W(#{Rails.root}/lib)

, . , , lib/ RSpec .

0

All Articles