spec/streaming_spec.rb in chatterbot-1.0.2 vs spec/streaming_spec.rb in chatterbot-2.0.0.pre

- old
+ new

@@ -1,110 +1,115 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper') -class StreamingHandler - attr_accessor :last_object -end - describe "Chatterbot::Streaming" do let(:bot) { test_bot } let(:user) { fake_user('user', 100) } - let(:handler) { StreamingHandler.new(test_bot) } let(:tweet) { fake_tweet(12345) } - def apply_to_handler(&block) - handler.apply block - end - describe "authenticated_user" do - it "should get user from client" do - expect(bot.client).to receive(:user).and_return('user') - expect(bot.authenticated_user).to eql('user') + describe "streaming_tweet_handler" do + before(:each) do + bot.skip_run = true + allow(bot.client).to receive(:user).and_return(user) end - end - describe "do_streaming" do + it "defaults to home_timeline" do + bot.register_handler(:home_timeline) { @result = :home_timeline } + bot.register_handler(:search, "foo") { @result = :search } - end + bot.handle_streaming_object(tweet) + expect(@result).to eql(:home_timeline) + end + it "will return search" do + bot.register_handler(:search, "foo") { @result = :search } + + bot.handle_streaming_object(tweet) + expect(@result).to eql(:search) + end + end + describe "handle_streaming_object" do before(:each) { + bot.skip_run = true allow(bot.client).to receive(:user).and_return(user) } describe "Twitter::Tweet" do it "works if no handler" do - bot.handle_streaming_object(tweet, handler) + bot.handle_streaming_object(tweet) end context "with handler" do before(:each) do - apply_to_handler { replies { |t| @last_object = t } } + bot.register_handler(:home_timeline) { |t| @last_object = t } end it "ignores tweets from authenticated user" do expect(tweet).to receive(:user).and_return(user) - bot.handle_streaming_object(tweet, handler) - expect(handler.last_object).to be_nil + bot.handle_streaming_object(tweet) + expect(@last_object).to be_nil end it "passes to handler" do - bot.handle_streaming_object(tweet, handler) - expect(handler.last_object).to eql(tweet) + bot.handle_streaming_object(tweet) + expect(@last_object).to eql(tweet) end - it "ignores tweets from blacklist" do - bot.blacklist = ['chatterbot'] - bot.handle_streaming_object(tweet, handler) - expect(handler.last_object).to be_nil + it "ignores tweets from blocklist" do + bot.blocklist = ['chatterbot'] + bot.handle_streaming_object(tweet) + expect(@last_object).to be_nil end it "ignores tweets if skip_me is true" do bot.exclude = ['tweet'] - bot.handle_streaming_object(tweet, handler) - expect(handler.last_object).to be_nil + bot.handle_streaming_object(tweet) + expect(@last_object).to be_nil end end end describe "Twitter::Streaming::DeletedTweet" do it "works if no handler" do obj = Twitter::Streaming::DeletedTweet.new(:id => 1) - bot.handle_streaming_object(obj, handler) + bot.handle_streaming_object(obj) end it "passes to handler" do - apply_to_handler { delete { |t| @last_object = t } } + bot.register_handler(:deleted) { |t| @last_object = t } obj = Twitter::Streaming::DeletedTweet.new(:id => 1) - bot.handle_streaming_object(obj, handler) - expect(handler.last_object).to eql(obj) + bot.handle_streaming_object(obj) + expect(@last_object).to eql(obj) end end describe "Twitter::DirectMessage" do it "works if no handler" do obj = Twitter::DirectMessage.new(:id => 1) - bot.handle_streaming_object(obj, handler) + bot.handle_streaming_object(obj) end it "passes to handler" do - apply_to_handler { direct_message { |t| @last_object = t } } + bot.register_handler(:direct_messages) { |t| @last_object = t } obj = Twitter::DirectMessage.new(:id => 1) - bot.handle_streaming_object(obj, handler) - expect(handler.last_object).to eql(obj) + bot.handle_streaming_object(obj) + expect(@last_object).to eql(obj) end end describe "Twitter::Streaming::Event" do it "ignores events generated by authenticated user" do event = Twitter::Streaming::Event.new( :event => :follow, :source => {:id => user.id, :name => 'name', :screen_name => 'name'}, :target => {:id => user.id, :name => 'name', :screen_name => 'name'}) - apply_to_handler { followed { |t| @last_object = t } } - bot.handle_streaming_object(event, handler) - expect(handler.last_object).to be_nil + bot.register_handler(:followed) { |t| @last_object = t } + + bot.handle_streaming_object(event) + expect(@last_object).to be_nil end describe "follow" do before(:each) do @event = Twitter::Streaming::Event.new( @@ -113,19 +118,19 @@ :target => {:id => user.id, :name => 'name', :screen_name => 'name'}) end it "works if no handler" do - bot.handle_streaming_object(@event, handler) - expect(handler.last_object).to be_nil + bot.handle_streaming_object(@event) + expect(@last_object).to be_nil end it "passes to handler" do - apply_to_handler { followed { |t| @last_object = t } } - bot.handle_streaming_object(@event, handler) - expect(handler.last_object.class).to be(Twitter::User) - expect(handler.last_object.id).to be(12345) + bot.register_handler(:followed) { |t| @last_object = t } + bot.handle_streaming_object(@event) + expect(@last_object.class).to be(Twitter::User) + expect(@last_object.id).to be(12345) end end describe "favorite" do before(:each) do @@ -140,33 +145,26 @@ :user => { :id => 1, :screen_name => "chatterbot" } }) end it "works if no handler" do - bot.handle_streaming_object(@event, handler) - expect(handler.last_object).to be_nil + bot.handle_streaming_object(@event) + expect(@last_object).to be_nil end it "passes to handler" do - apply_to_handler { favorited { |_, t| @last_object = t } } - bot.handle_streaming_object(@event, handler) - expect(handler.last_object.class).to be(Twitter::Tweet) - expect(handler.last_object.text).to eq("I am a tweet!") + bot.register_handler(:favorited) { |_, t| @last_object = t } + bot.handle_streaming_object(@event) + expect(@last_object.class).to be(Twitter::Tweet) + expect(@last_object.text).to eq("I am a tweet!") end end end describe "Twitter::Streaming::FriendList" do it "works if no handler" do obj = Twitter::Streaming::FriendList.new - bot.handle_streaming_object(obj, handler) - end - - it "passes to handler" do - apply_to_handler { friends { |t| @last_object = t } } - obj = Twitter::Streaming::FriendList.new - bot.handle_streaming_object(obj, handler) - expect(handler.last_object).to eql(obj) + bot.handle_streaming_object(obj) end end end end