Joseph Mate's Blog

Nested Contexts

Posted in Uncategorized by Joseph on January 31, 2010

I learned a new trick with shoulda. Credit goes to: http://www.viget.com/extend/reusing-contexts-in-shoulda-with-context-macros/

You can nest contexts inorder to resuse them.

Here is a quick example I prepared:

context "we have a temp dir" do
    setup
    do
        @temp_dir = "/tmp/temp_#{rand(1073741824)}"</pre>
        @log_dir = "#{@temp_dir}/log"
        @file = "#{@log_dir}/file"
        FileUtils.mkdir( @temp_dir )
    end
    teardown
    do
        FileUtils.rm_r( @temp_dir )
    end
    should "be able to test with the temp dir"
    do
    end

    context "we have a log dir"
    do
        setup
        do
            FileUtils.mkdir( @log_dir )
        end
        should "be able to test with the temp and log dir"
        do
        end

        context "we have a file inside the log dir inside the temp dir"
        do
            setup
            do
                FileUtils.touch( [@file] )
            end
            should "be able to test with temp, log and file"
            do
            end
        end
    end
end

Cheers,
Joseph

Advertisements

2 Responses

Subscribe to comments with RSS.

  1. […] Nested Contexts […]

  2. Joseph’s Midterm Report « UCOSP said, on March 1, 2010 at 12:59 am

    […] Shoulda [1] […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: