[rspec-users] rails /lib module testing

Elliot Winkler elliot.winkler at gmail.com
Mon Nov 16 20:38:47 EST 2009


That's because assigns only works in a Rails functional test.

If you're the author of the module, you could add a self.included block 
that then sets up an attr_reader :link, like so:

   def self.included(base)
     base.class_eval do
       attr_reader :link
     end
   end

Then, all you have to say is @link.link.should == Link.new.

Otherwise, you can access the instance variable within @link using 
instance_variable_get. So @link.instance_variable_get("@link").should == 
Link.new.

-- Elliot

On 11/16/09 1:32 PM, Andy Koch wrote:
> I've seen several posts related, but not touching on what I'm
> wondering...
>
> I have a lib module that I'd like to do 'white-box' testing - similar
> to a Rails mvc.  But I can't see how to get a handle on the internal
> instance vars in the method.
>
> here are some code snippets that probably best explain my situation...
>
> #########################################################
> require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
>
> describe AasmCrawler do
>
>    context "create_message method" do
>      it "should create a new Link message" do
>        @link.should == Link.new
>      end
>    end
> end
> #########################################################
> module AasmCrawler
>    def create_message
>      @link = Link.new
>    end
> end
> #########################################################
>
> I've tried "assigns[:link]" but "assigns" is not known.
>
> I've also seen comments to test this via a model where it is included
> - but that seems like WET testing, if this is used repeatedly why not
> test it once at the source?
>
> thanks for any help
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users


More information about the rspec-users mailing list