Rails: How to test code in the lib/ directory?

前端 未结 7 1500

I have a model which gets its data from a parser object. I\'m thinking that the parser class should live in the lib/ directory (although I could be persuaded that it should live

7条回答
  •  梦如初夏
    2021-01-30 07:22

    Here's one way:

    Create lib/tasks/test_lib_dir.rake with the following

    namespace :test do
    
      desc "Test lib source"
      Rake::TestTask.new(:lib) do |t|    
        t.libs << "test"
        t.pattern = 'test/lib/**/*_test.rb'
        t.verbose = true    
      end
    
    end
    

    Mimic the structure of your lib dir under the test dir, replacing lib code with corresponding tests.

    Run rake test:lib to run your lib tests.

    If you want all tests to run when you invoke rake test, you could add the following to your new rake file.

    lib_task = Rake::Task["test:lib"]
    test_task = Rake::Task[:test]
    test_task.enhance { lib_task.invoke }
    

提交回复
热议问题