From 59dd4645c422a2d914b87a48854649c6c2b744b5 Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Sat, 25 Aug 2018 20:19:06 -0400 Subject: [PATCH] Refactor Clarifai specs --- spec/features/user_manages_image_spec.rb | 19 +++++++++++++++---- spec/lib/clarifai_spec.rb | 22 +++++++++++++++++++--- spec/rails_helper.rb | 2 +- spec/support/clarifai_helpers.rb | 16 +++------------- 4 files changed, 38 insertions(+), 21 deletions(-) diff --git a/spec/features/user_manages_image_spec.rb b/spec/features/user_manages_image_spec.rb index 50eaf6d..1839913 100644 --- a/spec/features/user_manages_image_spec.rb +++ b/spec/features/user_manages_image_spec.rb @@ -1,7 +1,11 @@ require "rails_helper" describe "user manages images" do + include ActiveJob::TestHelper + it "uploads the image" do + stub_clarifai(%w[computer technology]) + sign_in attach_file("Image", "#{Rails.root}/spec/fixtures/spectrum.jpg") click_on("Create Image") @@ -19,11 +23,18 @@ describe "user manages images" do expect(page).not_to have_css(".image") end - it "views the image" do - user = User.create(uid: 1) - Image.create(user: user, image: "#{Rails.root}/spec/fixtures/spectrum.jpg") + it "views the image with tags" do + stub_clarifai(%w[computer technology]) sign_in - find(".image").click + attach_file("Image", "#{Rails.root}/spec/fixtures/spectrum.jpg") + perform_enqueued_jobs do + click_on("Create Image") + end + page.find(".image").click + + %w[computer technology].each do |tag| + expect(page).to have_content(tag) + end end end diff --git a/spec/lib/clarifai_spec.rb b/spec/lib/clarifai_spec.rb index ff1c056..cef4a8d 100644 --- a/spec/lib/clarifai_spec.rb +++ b/spec/lib/clarifai_spec.rb @@ -9,11 +9,27 @@ describe Clarifai do describe ".tags" do it "predicts tags for our image" do - stub_clarifai(%w[computer technology]) + stub_api(%w[computer technology]) - clarifai_image = Clarifai.new("spec/fixtures/spectrum.jpg") + clarifai = Clarifai.new("spec/fixtures/spectrum.jpg") - expect(clarifai_image.tags).to eq(%w[computer technology]) + expect(clarifai.tags).to eq(%w[computer technology]) + end + end + + def stub_api(tags) + WebMock. + stub_request(:post, Clarifai::API_URL). + to_return(status: 200, body: stub_body(tags).to_json) + end + + def stub_body(tags) + { "outputs": [{ "data": { "concepts": tag_structure(tags) } }] } + end + + def tag_structure(tags) + tags.map do |tag| + { "name": tag, "value": 0.95 } end end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index f5522e0..343cc32 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -65,7 +65,7 @@ RSpec.configure do |config| # config.filter_gems_from_backtrace("gem name") config.include AuthHelpers, type: :feature - config.include ClarifaiHelpers, type: :job + config.include ClarifaiHelpers end Shoulda::Matchers.configure do |config| diff --git a/spec/support/clarifai_helpers.rb b/spec/support/clarifai_helpers.rb index 1d864be..6e1417d 100644 --- a/spec/support/clarifai_helpers.rb +++ b/spec/support/clarifai_helpers.rb @@ -1,17 +1,7 @@ module ClarifaiHelpers def stub_clarifai(tags) - WebMock. - stub_request(:post, Clarifai::API_URL). - to_return(status: 200, body: stub_body(tags).to_json) - end - - def stub_body(tags) - { "outputs": [{ "data": { "concepts": tag_structure(tags) } }] } - end - - def tag_structure(tags) - tags.map do |tag| - { "name": tag, "value": 0.95 } - end + clarifai_mock = instance_double(Clarifai) + allow(clarifai_mock).to receive(:tags).and_return(tags) + allow(Clarifai).to receive(:new).and_return(clarifai_mock) end end