diff --git a/spec/models/services/twitter_spec.rb b/spec/models/services/twitter_spec.rb new file mode 100644 index 00000000..352ed369 --- /dev/null +++ b/spec/models/services/twitter_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe Services::Twitter do + describe "#post" do + let(:user) { FactoryBot.create(:user) } + let(:service) { Services::Twitter.create(user: user) } + let(:answer) { FactoryBot.create(:answer, user: user, + content: 'a' * 255, + question_content: 'q' * 255) } + let(:twitter_client) { instance_double(Twitter::REST::Client) } + + before do + allow(Twitter::REST::Client).to receive(:new).and_return(twitter_client) + allow(twitter_client).to receive(:update!) + stub_const("APP_CONFIG", { + 'hostname' => 'example.com', + 'https' => true, + 'items_per_page' => 5, + 'sharing' => { + 'twitter' => { + 'consumer_key' => 'AAA', + } + } + }) + end + + it "posts a shortened tweet" do + service.post(answer) + + expect(twitter_client).to have_received(:update!).with("#{'q' * 123}… — #{'a' * 124}… https://example.com/#{user.screen_name}/a/#{answer.id}") + end + end +end \ No newline at end of file