JSONAPI testing with rspec and Airborne - ruby-on-rails

Hello i have problem with testing JSONAPI with rspec and airborne.
GET model below
https://i.stack.imgur.com/Cyf75.png
Im testing it this way https://i.stack.imgur.com/Y9rHt.png
Rspec output:
Failures:
1) GET on /contacts should validate types
Failure/Error: expect_json('books.0', title: 'The Great Escape')
Airborne::PathError:
Expected NilClass
to be an object with property 0
# /home/robert/.rvm/gems/ruby-2.4.0/gems/airborne-0.2.5/lib/airborne/path_matcher.rb:21:in `rescue in block in get_by_path'
# /home/robert/.rvm/gems/ruby-2.4.0/gems/airborne-0.2.5/lib/airborne/path_matcher.rb:18:in `block in get_by_path'
# /home/robert/.rvm/gems/ruby-2.4.0/gems/airborne-0.2.5/lib/airborne/path_matcher.rb:9:in `each'
# /home/robert/.rvm/gems/ruby-2.4.0/gems/airborne-0.2.5/lib/airborne/path_matcher.rb:9:in `each_with_index'
# /home/robert/.rvm/gems/ruby-2.4.0/gems/airborne-0.2.5/lib/airborne/path_matcher.rb:9:in `get_by_path'
# /home/robert/.rvm/gems/ruby-2.4.0/gems/airborne-0.2.5/lib/airborne/request_expectations.rb:137:in `call_with_path'
# /home/robert/.rvm/gems/ruby-2.4.0/gems/airborne-0.2.5/lib/airborne/request_expectations.rb:18:in `expect_json'
# ./book_resource.rb:10:in `block (2 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# NoMethodError:
# undefined method `[]' for nil:NilClass
# /home/robert/.rvm/gems/ruby-2.4.0/gems/airborne-0.2.5/lib/airborne/path_matcher.rb:57:in `process_json'
Finished in 0.03121 seconds (files took 0.17681 seconds to load)
1 example, 1 failure

Your API response doesn't have the key books. Instead, it is returning the response as { "data": [ ... ] }.
So, in your tests, you need to specify expect_json('data.0', ...), instead of expect_json('books.0', ...).
Hope that should resolve this issue.

Already solve with:
describe Api::UsersController do
describe 'GET on /users' do
before do
FactoryGirl.create('user', name: 'Jack McClure')
FactoryGirl.create('user', name: 'Johny Reaper')
FactoryGirl.create('user', name: 'Niko Bellic')
end
it 'should return valid user' do
get :index, format: 'json'
expect_json('data.0.attributes', name: "Jack McClure")
expect_json('data.2.attributes', name: 'Niko Bellic')
end
end

Related

rspec spec fails only when run in entire test suite - passes when run individually

I have an almost meaningless worker test spec:
require 'rails_helper'
require 'sidekiq/testing'
Sidekiq::Testing.fake!
RSpec.describe FetchArticleContentWorker, type: :worker do
describe "Sidekiq Worker" do
before(:each) do
allow_any_instance_of(DataSource).to receive(:subscribe).and_return(true)
allow_any_instance_of(FetchArticleContentWorker).to receive(:perform).and_return(true)
end
let(:user) { FactoryBot.create(:user) }
let(:data_source) { FactoryBot.create(:data_source) }
let(:article) { FactoryBot.create(:article, data_source_id: data_source.id) }
it "should respond to #perform" do
expect(FetchArticleContentWorker.new).to respond_to(:perform)
end
describe "fetch article content worker" do
before do
Sidekiq::Worker.clear_all
end
it "increase the worker job size" do
expect { FetchArticleContentWorker.perform_async(article.url) }.to change(FetchArticleContentWorker.jobs, :size).by(1)
end
it "assert that jobs were pushed on to the queue" do
assert_equal 0, FetchArticleContentWorker.jobs.size
FetchArticleContentWorker.perform_async(article.url)
assert_equal 1, FetchArticleContentWorker.jobs.size
end
it "job runs successfully" do
expect(FetchArticleContentWorker.new.perform(article.url)).to eq(true)
end
end
end
end
For whatever reason, this set of specs has suddenly started failing but only when run as a part of the entire test suite. If I run this test directly by itself, it passes every time.
When run with bundle exec rspec (to do the whole suite), I get the following failures for this particular test:
Failures:
1) FetchArticleContentWorker Sidekiq Worker fetch article content worker increase the worker job size
Failure/Error: expect { FetchArticleContentWorker.perform_async(article.url) }.to change(FetchArticleContentWorker.jobs, :size).by(1)
expected `Array#size` to have changed by 1, but was changed by 0
# ./spec/workers/fetch_article_content_worker_spec.rb:29:in `block (4 levels) in <top (required)>'
# ./spec/rails_helper.rb:48:in `block (3 levels) in <top (required)>'
# /usr/local/bundle/gems/database_cleaner-1.8.4/lib/database_cleaner/generic/base.rb:16:in `cleaning'
# /usr/local/bundle/gems/database_cleaner-1.8.4/lib/database_cleaner/configuration.rb:87:in `block (2 levels) in cleaning'
# /usr/local/bundle/gems/database_cleaner-1.8.4/lib/database_cleaner/configuration.rb:88:in `cleaning'
# ./spec/rails_helper.rb:47:in `block (2 levels) in <top (required)>'
2) FetchArticleContentWorker Sidekiq Worker fetch article content worker assert that jobs were pushed on to the queue
Failure/Error: assert_equal 1, FetchArticleContentWorker.jobs.size
Minitest::Assertion:
Expected: 1
Actual: 0
# /usr/local/bundle/gems/minitest-5.14.0/lib/minitest/assertions.rb:183:in `assert'
# /usr/local/bundle/gems/minitest-5.14.0/lib/minitest/assertions.rb:218:in `assert_equal'
# ./spec/workers/fetch_article_content_worker_spec.rb:35:in `block (4 levels) in <top (required)>'
# ./spec/rails_helper.rb:48:in `block (3 levels) in <top (required)>'
# /usr/local/bundle/gems/database_cleaner-1.8.4/lib/database_cleaner/generic/base.rb:16:in `cleaning'
# /usr/local/bundle/gems/database_cleaner-1.8.4/lib/database_cleaner/configuration.rb:87:in `block (2 levels) in cleaning'
# /usr/local/bundle/gems/database_cleaner-1.8.4/lib/database_cleaner/configuration.rb:88:in `cleaning'
# ./spec/rails_helper.rb:47:in `block (2 levels) in <top (required)>'
It appears that these worker specs only fail when a feature using turnip is previously run.
I ended up finding what I think is the problem.
In my turnip steps file I was setting sidekick to inline:
# set sidekiq to inline
require 'sidekiq/testing'
Sidekiq::Testing.inline!
And, even though I was setting Sidekiq::Testing.fake! in the above mentioned spec file, it didn't seem to "take".
When I moved fake! into the before block, that seemed to fix the problem:
describe "Sidekiq Worker" do
before(:each) do
allow_any_instance_of(DataSource).to receive(:subscribe).and_return(true)
allow_any_instance_of(FetchArticleContentWorker).to receive(:perform).and_return(true)
Sidekiq::Testing.fake!
end
...
I am not sure why this is the fix, but this fixed it.

RSpec - A JSON text must at least contain two octets

Two failing specs after upgrading rspec-rails (2.5.2 -> 3.8.1) and capybara (2.18.0 -> 3.10.1):
Not really sure what's going on here. Looks like text in the expectation is being truncated?!?
let(:story_attributes) do
{
title: 'Edited title',
description: 'Edited location',
start_year: '2001',
start_month: 'December',
start_day: '5',
end_year: '2001',
end_month: 'October',
end_day: '10',
is_range: true,
cover_image: {
url: 'http://placehold.it/edited.png'
}
}
end
...
within 'section.story-cover' do
expect(page).to have_text 'Edited title'
expect(page).to have_text 'Edited location'
expect(page).to have_text 'December 5th - October 10th, 2001'
end
In first failed example (below) "Edited location" is being truncated.
In second example expect(page).to have_text 'Edited title Edited location' where only "Edited title\nEdited locat" is found.
Then there's this "JSON text must at least contain two octets" issue which may or may not be related but this used to pass before upgrading rspec-rails & capybara.ds
Thoughts?
RSpec Failures:
1) Story editing published edit story
Failure/Error: JSON.parse(response.body)
JSON::ParserError:
A JSON text must at least contain two octets!
# ./app/services/converter/image_service.rb:36:in `post_to_filepicker'
# ./app/services/converter/image_service.rb:18:in `convert_format'
# ./app/services/converter/image_service.rb:11:in `block in convert'
# ./app/services/converter/image_service.rb:10:in `each'
# ./app/services/converter/image_service.rb:10:in `convert'
# ./app/models/images/image.rb:5:in `convert'
# ./app/models/images/image.rb:20:in `enqueue_conversion'
# ./app/services/story/updating_service.rb:14:in `update'
# ./app/controllers/stories_controller.rb:58:in `update'
# ------------------
# --- Caused by: ---
# Capybara::ExpectationNotMet:
# expected to find text "Edited location" in "Edited title\nLyla HoegerEditedDecember 5th - October 10th, 2001Download"
# ./spec/features/stories/editing_spec.rb:86:in `block (4 levels) in <top (required)>'
2) Story editing private private story should be read after editing
Failure/Error: JSON.parse(response.body)
JSON::ParserError:
A JSON text must at least contain two octets!
# ./app/services/converter/image_service.rb:36:in `post_to_filepicker'
# ./app/services/converter/image_service.rb:18:in `convert_format'
# ./app/services/converter/image_service.rb:11:in `block in convert'
# ./app/services/converter/image_service.rb:10:in `each'
# ./app/services/converter/image_service.rb:10:in `convert'
# ./app/models/images/image.rb:5:in `convert'
# ./app/models/images/image.rb:20:in `enqueue_conversion'
# ./app/services/story/updating_service.rb:14:in `update'
# ./app/controllers/stories_controller.rb:58:in `update'
# ------------------
# --- Caused by: ---
# Capybara::ExpectationNotMet:
# expected to find text "Edited title Edited location" in "Edited title\nEdited locat"
# ./spec/features/stories/editing_spec.rb:115:in `block (4 levels) in <top (required)>'
One of the big changes between Capybara 2.x and 3.x was that in Capybara 3.x text is returned as closely to what is displayed as possible. This means that line feeds are now included in the returned text when they would display to the user - https://github.com/teamcapybara/capybara/blob/master/UPGRADING.md. You either need to change your expected text to "Edited title\nEdited location" at spec/features/stories/editing_spec.rb:115 or if you don't care about the linefeeds you can use the :normalize_ws option => expect(page).to have_text("Edited title Edited location", normalize_ws: true)

Rspec controller test assign two dimensional Hash fails

i'm trying to run a controller test:
the controller code is
def aix_service_accounts
#no_dn_users= Hash.new
record = 0
#no_dn_users[record]= Hash.new
#no_dn_users[record]['aix_username'] ="abc"
end
The rspec code is
it "should show aix_service_accounts" do
get :aix_service_accounts
expect(assigns(:no_dn_users[0]['aix_username'])).to eq 'abc'
end
The result is
Failures:
1) AixController should show aix_service_accounts
Failure/Error: expect(assigns(:no_dn_users[0]['aix_username'])).to eq 'abc'
expected: "abc"
got: {"marked_for_same_origin_verification"=>true, "no_dn_users"=>{0=>{"aix_username"=>"abc"}}}
(compared using ==)
Diff:
## -1,2 +1,3 ##
-"abc"
+"marked_for_same_origin_verification" => true,
+"no_dn_users" => {0=>{"aix_username"=>"abc"}},
# ./spec/controllers/aix_controller_spec.rb:29:in `block (2 levels) in <top (required)>'
Could anyone help and explain the reason to me? Thanks!
Sure - you're trying to ask for a single key called :no_dn_users[0]['aix_username'] from the assigns. You need to change that to:
assigns(:no_dn_users)[0]['aix_username']
instead.
Ie, get out the first part by the key :no_dn_users, this will be the hash-inside-an-array that you're then referencing using indexing

How to stub using any_instance in Rspec feature spec with Capybara?

I have the following in a feature spec:
it "shows the page" do
Project.any_instance.stub(:price_all)
login_user
gso = create(:gs_option)
gso.gs_collector.collector.project.update(user_id: #user.id)
visit edit_gs_option_path(gso)
end
Yet, it always fails because the price_all method on Project is not being stubbed. The failure trace contains the following:
# ./app/models/project.rb:430:in `price_all'
How do I stub the price_all method on the Project class?
I've tried stub(:price_all).with(anything()) and stub(:price_all).with(any_args()), but it doesn't change the failure message.
Here's the full failure:
1) GS Options page shows the page
Failure/Error: visit edit_gs_option_path(gso)
NoMethodError:
undefined method `id' for nil:NilClass
# ./app/models/collector.rb:435:in `price_item'
# ./app/models/gs_collector.rb:279:in `price_out'
# ./app/models/collector.rb:260:in `price_out_all'
# ./app/models/project.rb:430:in `price_all'
# ./app/controllers/application_controller.rb:187:in `get_totals'
# ./app/controllers/gs_options_controller.rb:6:in `edit'
# ./spec/features/gs_options_spec.rb:10:in `block (2 levels) in <top (required)>'
This stubbing feature started working when I upgraded to rspec-rails 3.0.0.beta.

rspec testing strong params and building a model

I am attempting to write a model test, like so:
require 'spec_helper'
describe Five9List do
before :each do
#five9_list = Five9List.new(name: 'test_list', size: '100')
end
describe "#new" do
it "takes two parameters and returns a Five9List object" do
#five9_list.should be_an_instance_of Five9List
end
end
describe "#name" do
it "returns the correct name" do
#five9_list.name.should eql "test_list"
end
end
describe "#size" do
it "returns the correct size" do
#five9_list.size.should eql 100
end
end
end
Currently, this succeeds and works fine. That's because my model is using attr_accessible, like so:
class Five9List < ActiveRecord::Base
attr_accessible :name, :size
end
If I want to get rid of attr_accessible and follow the rails 4 convention of using strong_params, how would I write that to where my rspec test would still succeed?
Adding this in my controller:
private
def five9_list_params
params.require(:five9_list).permit(:name, :size)
end
And removing attr_accessible does not work.
EDIT
Here is the error I receive from rspec .:
Failures:
1) Five9List#name returns the correct name
Failure/Error: #five9_list.name.should eql "test_list"
expected: "test_list"
got: nil
(compared using eql?)
# ./spec/models/five9_list_spec.rb:16:in `block (3 levels) in <top (required)>'
2) Five9List#size returns the correct size
Failure/Error: #five9_list.size.should eql 100
expected: 100
got: nil
(compared using eql?)
# ./spec/models/five9_list_spec.rb:22:in `block (3 levels) in <top (required)>'
Finished in 0.03303 seconds
4 examples, 2 failures, 1 pending
Failed examples:
rspec ./spec/models/five9_list_spec.rb:15 # Five9List#name returns the correct name
rspec ./spec/models/five9_list_spec.rb:21 # Five9List#size returns the correct size
Randomized with seed 20608
There's nothing wrong with your spec. I can only guess that you're not running Rails 4 or you've installed the ProtectedAttributes gem.

Resources