Correct way to add helper functions for an rspec spec

NNNNNNNNNNDelicious picture NNNNNNNNNNDelicious · Sep 16, 2015 · Viewed 10.8k times · Source

So I need a helper function to created 'unprocessed tweets' similar to how I might receive them from the Twitter API gem, so I can test my models functionality under certain conditions.

To do this I added a helper function inside my objects describe, like so:

describe Tag, :type => :model do 
# Helpers
    ###
    def unprocessed_tweets(count, name, start_date, end_date)
        tweets = []

        count.times do |index|
            tweet = OpenStruct.new

            tweet.favorite_count = "3"
            tweet.filter_level = "high"
            tweet.retweet_count = "12" 
            tweet.text = "#{name}"

            if index == 0
                tweet.created_at = start_date
            elsif index == (count-1)
                tweet.created_at = end_date
            else
                tweet.created_at = start_date
            end

            tweets.push tweet
        end

        tweets
    end

I also added a test to make sure my helpers doing as I expect in the long term:

    it "has a helper for generated unprocessed tweets" do 

        tag_name = "justin"
        start_date = '2015-09-12 2:31:32 0'
        end_date = '2015-09-13 2:31:32 0'

        tweets = unprocessed_tweets(3, tag_name, start_date, end_date)

        expect(tweets.size).to eq 3
        expect(tweets.first.favorite_count).to eq "3"
        expect(tweets.first.created_at).to eq start_date
        expect(tweets.last.created_at).to eq end_date
        expect(tweets.last.text).to eq tag_name
    end

Is this best practice for this?

Answer

K M Rakibul Islam picture K M Rakibul Islam · Sep 16, 2015

You can create a new file in spec/support called tweet_helpers.rb and put this content in it:

module TweetHelpers
  def unprocessed_tweets(count, name, start_date, end_date)
    tweets = []

    count.times do |index|
      tweet = OpenStruct.new

      tweet.favorite_count = "3"
      tweet.filter_level = "high"
      tweet.retweet_count = "12"
      tweet.text = "#{name}"

      if index == 0
        tweet.created_at = start_date
      elsif index == (count-1)
        tweet.created_at = end_date
      else
        tweet.created_at = start_date
      end

      tweets.push tweet
    end

    tweets
  end
end

And your spec test file should look like this:

require './spec/support/tweet_helpers'

RSpec.configure do |c|
  c.include TweetHelpers
end

RSpec.describe "an example group" do
  it "has a helper for generated unprocessed tweets" do

    tag_name = "justin"
    start_date = '2015-09-12 2:31:32 0'
    end_date = '2015-09-13 2:31:32 0'

    tweets = unprocessed_tweets(3, tag_name, start_date, end_date)

    expect(tweets.size).to eq 3
    expect(tweets.first.favorite_count).to eq "3"
    expect(tweets.first.created_at).to eq start_date
    expect(tweets.last.created_at).to eq end_date
    expect(tweets.last.text).to eq tag_name
  end
end

I think this is good practice to define your helper methods in a separate module rather than crowding the spec test file itself.

See this for more information and examples.