Testing a ruby class without the required module files -


i'm working on project in ruby on rails. have controller action uses module within gem. gem isn't finished yet , isn't on file system.

i told mock module in order test controller. there way test without actual gem? mocking 'require' calls work? using mocha mocking , stubbing.

there way mock imports in python. maybe there similar answer mocking requires in ruby. how mock import

or please let me know best way handle this.

update: person told me mock it, suggested adding stub file, require adding test code controller , don't want that.

update 2: controller uses methods declared in module.

if writing tests mock method calls, fail. example,

controller.should_receive(:method_in_non_existent_module).with(args) #=> errors 

in correct red->green tdd scenario , alright because next step require gem/file, include module , add method call in controller , make test pass. you'll not able make tests pass since can't require file because doesn't exist yet.

may developer asked mock method meant not in tests, in actual code. example, he's writing gem 'dongle-jokes' has method gets popular dongle joke recent tech conference. doesn't want gem blocker finish controller , views asks use dummy interface spits out dummy response.

  1. write tests fail
  2. add file lib/dongle-jokes.rb
  3. add following file.

    module donglejokes   def joke     "dongle jokes aren't funny!"   end end 
  4. require file , include module, use method in controller.

the test should pass now. can remove lib/dongle-jokes.rb when start using actual gem.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -