Can Rspec do module spec? What if module uses test data?

View: New views
2 Messages — Rating Filter:   Alert me  

Can Rspec do module spec? What if module uses test data?

by Shaker :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Dear fellows:
    I am currently working on writing specs on some ruby modules(e.g. myModule). At first I try to use script/generate to generate rspec_module, but it failed.
    Later I manually created a file (e.g. named myModule_spec.rb) and put the 'require ....spec_helper.rb' as the first line of code followed by 'describe' block. Luckily, it seemed to be working. But it was not likely to be a logical way to generate module specs.
    Question 1:Is there any better way of creating module spec??
    Anyway, I started to write method specs in these manually created module specs, and I found another problem. Module specs do not support 'fixtures'. This means that if you test some methods which modify test data in your module specs, that data won't be restored after you finish running each spec. In other words, all the modules specs actually share a copy of test data. This will cause some module specs to fail if other modules do modified the shared data beforehand.
    Question 2:What should I do to resolve this problem, making module specs access test data independently?

Re: Can Rspec do module spec? What if module uses test data?

by Aslak Hellesoy :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Here is an example from RSpec's own specs:

    describe Enumerable do
      def each(&block)
        ["4", "2", "1"].each(&block)
      end

      it "should be included in examples because it is a module" do
        map{|e| e.to_i}.should == [4,2,1]
      end
    end

If you pass the module to describe it will automatically be included
in each of your it blocks, so you're right at home!

HTH,
Aslak

On 8/22/07, Shaker <flyeminent@...> wrote:

>
> Dear fellows:
>     I am currently working on writing specs on some ruby modules(e.g.
> myModule). At first I try to use script/generate to generate rspec_module,
> but it failed. :-(
>     Later I manually created a file (e.g. named myModule_spec.rb) and put
> the 'require ....spec_helper.rb' as the first line of code followed by
> 'describe' block. Luckily, it seemed to be working. But it was not likely to
> be a logical way to generate module specs.
>     Question 1:Is there any better way of creating module spec??:confused:
>     Anyway, I started to write method specs in these manually created module
> specs, and I found another problem. Module specs do not support 'fixtures'.
> This means that if you test some methods which modify test data in your
> module specs, that data won't be restored after you finish running each
> spec. In other words, all the modules specs actually share a copy of test
> data. This will cause some module specs to fail if other modules do modified
> the shared data beforehand.
>     Question 2:What should I do to resolve this problem, making module specs
> access test data independently? :confused:
> --
> View this message in context: http://www.nabble.com/Can-Rspec-do-module-spec--What-if-module-uses-test-data--tf4309226.html#a12267615
> Sent from the rspec-users mailing list archive at Nabble.com.
>
> _______________________________________________
> rspec-users mailing list
> rspec-users@...
> http://rubyforge.org/mailman/listinfo/rspec-users
>
_______________________________________________
rspec-users mailing list
rspec-users@...
http://rubyforge.org/mailman/listinfo/rspec-users