5

Possible Duplicate:
Testing modules in rspec

Currently I am successfully testing my modules with rspec like so:

require 'spec_helper'

module Services
  module AppService
    describe AppService do
      describe "authenticate" do
        it "should authenticate the user" do
          pending "authenticate the user"
        end
      end
    end
  end
end

My modules live in app/services/services.rb app/services/app_service.rb

However is there a more elegant solution to testing my modules without having to declare the namespaces in the spec? I feel that it becomes tightly bound to my specs and making changes will cause a lot of headaches.

Community
  • 1
  • 1
Andre Dublin
  • 1,148
  • 1
  • 16
  • 34
  • See http://stackoverflow.com/questions/1542945/testing-modules-in-rspec – Benissimo Oct 10 '12 at 18:44
  • 3
    This is not a duplicate of question 1542945, this questions regards nested modules not testing modules in general. – David Basarab Oct 10 '12 at 20:38
  • @Deefour - as David Basarab mentioned, this is not a duplicate of 1542945. This is a good question and I cannot find a real duplicate. – Powers Sep 07 '14 at 12:11

1 Answers1

12

Instead of duplicating AppServices, you can do:

module Services
  describe AppService do
  end
ene

Or:

describe Services::AppService do
end

I tend to prefer the former, because it allows me to not fully-qualify constant names in the spec (since everything is in the Services module, it'll look relative to that, first).

Myron Marston
  • 21,452
  • 5
  • 64
  • 63
  • I tried both methods and I get a load error https://gist.github.com/3869226. My namespace is as follows https://gist.github.com/3869225. Do I need to require the Service module? that seems tightly coupled... – Andre Dublin Oct 10 '12 at 23:37
  • ok, so I did a require_relative and it worked out for me. But I feel that I should be able to do this w/o requiring the file... – Andre Dublin Oct 11 '12 at 00:14
  • You've wandered into the zone of Rails' autoloading. I personally hate auto loading and much prefer requiring my files where they are used. It helps me have faster tests (each test can run in isolation with just what it needs loaded) and helps to surface points of coupling--I have to explicitly load a dependency because it won't be magically autoloaded for me. – Myron Marston Oct 11 '12 at 03:59
  • So I've finally decided to move my services into the lib/services folder minus the service.rb file/module. But I can understand where you are coming from with autoloading. However in my tests are separate and not as dependent upon each other. When I need to test a module I have to include the module I need, so I guess I could say that it is a success with out binding my tests. – Andre Dublin Oct 11 '12 at 22:50