Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4627

How do I call a module method in rspec, with the current module set up

$
0
0

So I have the following module for handling the fetching of and saving of tweets. This is all done on a thread, I am interested in testing two methods with in this module: fetch_tweets and save_tweets

For fetch_tweets, I want to validate that the save_tweets method is called. I don't care what it returns.

For the save_tweets, I want to test that a tweet is actually saved.

So with that in mind, lets look at some code.

Twitter::TwitterHandler - This lives in lib/twitter/twitter_handler.rb

module Twitter  module TwitterHandler    def handle_tweets(twitter_client_object)      Thread.new do        begin          twitter_lock(twitter_client_object)        ensure          ActiveRecord::Base.clear_active_connections!        end      end    end    def twitter_lock(twitter_client_object)      TwitterTweet.with_advisory_lock('aisis-writer-tweets', 0) do        fetch_tweets(twitter_client_object)      end    end    def fetch_tweets(twitter_client_object)      Rails.cache.fetch([:aisis_twitter_feed, Time.now.to_i/60/5], expires_in: 1.minute){        twitter_client.user_timeline(user_id: '252157965', count: 100).map(&:as_json).select{ |h| h["text"].match(/aisiswriter/i) }.take(10).each do |tweet|          save_tweets(tweet['id_str'], tweet['text'], tweet['created_at'])        end      }    end    def save_tweets(id, text, created_at)      tweet = TwitterTweet.find_or_initialize_by(id: id)      tweet.text = text      tweet.created_at = created_at      tweet.save    end  endend

twitter_handler_spec.rb - This lives in spec/twitter/twitter_handler_spec.rb

require 'rails_helper'require_relative '../../lib/twitter/twitter_handler'describe 'TwitterHandler' do  context "fetch tweets method" do    it "should fetch some tweets" do      t = Time.parse("01/01/2010 10:00")      Time.should_receive(:now).and_return(t)      expect(fetch_tweets(twitter_client)).to receive(:save_tweets).with('342', 'Something', t).and_return nil    end  endend

When I run bin/rspec spec/twitter/twitter_handler_spec.rb I get:

Failures:  1) TwitterHandler fetch tweets method should fetch some tweets     Failure/Error: expect(fetch_tweets(twitter_client)).to receive(:save_tweets).with('342', 'Something', t).and_return nil     NoMethodError:       undefined method `fetch_tweets' for #<RSpec::ExampleGroups::TwitterHandler::FetchTweetsMethod:0x007ff1c724e300>     # ./spec/twitter/twitter_handler_spec.rb:11:in `block (3 levels) in <top (required)>'

How do I fix this, so I can call this method and any other method in this module?


Viewing all articles
Browse latest Browse all 4627

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>