In other words: It there a chance for a class modification (on tests) to affect production code?
(This code example is using Rspec for testing in a Rails app)
My controller example
In this controller ExternalModel is created. Then it's "inscription" method is called and the results are assigned to a variable. It uses the result for other actions on the controller method.
class ExampleController < ApplicationController
def callback_page
external_model = ExternalModel.new(argument)
result = external_model.inscription
render_error_json && return unless result['error_desc'].eql? 'OK'
TransactionModel.create(token: result['token'])
end
end
My Spec example
In the spec I modify ExternalModel so it returns what I want when calling the .inscription method:
ExternalModel.class_eval {
def inscription(_fake_arguments)
{
'error_desc' => 'OK',
'token' => '1234'
}
end
}
This is the entire spec:
RSpec.describe 'Example management', type: :request do
context 'callback_page' do
it 'creates a transaction' do
ExternalModel.class_eval {
def inscription(_fake_arguments)
{
'error_desc' => 'OK',
'token' => '1234'
}
end
}
expect {
post(callback_page_path)
}.to change(TransactionModel.all, :count).by(1)
expect(response).to render_template(:callback_page)
end
end
end
What you're trying to achieve here is exactly what stubs are for: They're effectively a way to fake behavior within the scope of a single example that then automatically resets to its original behavior after the example has run.
In your example, this would look roughly like this:
allow_any_instance_of(ExternalModel).
to receive(:inscription).
and_return({ 'error_desc' => 'OK', 'token' => '1234' })
More details can be found in the docs for the rspec-mocks gem: https://relishapp.com/rspec/rspec-mocks/v/3-9/docs.
In one of my tests I have to verify that a certain offer returns 404 response if the available limit (10 seats for example with that offer) are all sold, or it has expired which ever comes first.
I just cannot get the PUT request to work in Request Spec, here's my code
RSpec.describe "Offers", type: :request do
describe "gives not found response" do
it "when available limit exhausts before date till available" do
offer = Offer.new
Timecop.freeze(Date.today - 12.days) do
offer = FactoryGirl.create(:offer)
end
payload = FactoryGirl.attributes_for(:offer, :available_limit => 0, :discount_id => offer.discount.id, :coupon_id => offer.coupon.id)
sign_in
put '/offers/'<<offer.id, params: { id: offer.id, offer: payload }, as: :json
get "/vouchers/"<<offer.coupon.voucher_code
expect(response.status).to eq 404
end
end
end
needles to say that I have tried many hacks including
put '/offers/'<<offer.id.to_s<<".json", params: { offer: payload }
or even
put '/offers/'<<offer.id.to_s<<".json", payload
What I also noticed was that in one of the combinations the request did go through but it responds with both HTML and JSON format which lead to error in the spec as I am not running them under capybara (and I do not want to either)
Don't use << to build those paths. Using << with an id will insert a character code equivalent to the integer value of the id, e.g.
'XYZ' << 123 << 'ABC'
=> "XYZ{ABC"
Just use normal string interpolation, e.g.
put "/offers/#{offer.id}", params: { id: offer.id, offer: payload }, format: :json
get "/vouchers/#{offer.coupon.voucher_code}"
Credit to #house9 for noticing the format: :json part as well.
I think you want to use format: :json not as: :json
Try:
params = { id: offer.id, offer: payload }
put :offers, params: params, format: :json
The following code tests image validation within a model spec in a Rails 4.2 app with RSpec 3.5 and the Shrine gem for file uploads.
My questions are:
Can you think of a way to improve the following tests or a better way to test those validations?
How to improve the speed of the file size validation test? If possible, I'd like to test it without actually having to upload a >10mb file.
Other aspects of the file upload setup are tested in controller and feature specs, which are irrelevant to this question.
RSpec.describe ShareImage, :type => :model do
describe "#image", :focus do
let(:image_file) do
# Could not get fixture_file_upload to work, but that's irrelevant
Rack::Test::UploadedFile.new(File.join(
ActionController::TestCase.fixture_path, 'files', filename))
end
let(:share_image) { FactoryGirl.build(:share_image, image: image_file) }
before(:each) { share_image.valid? }
context "with a valid image file" do
let(:filename) { 'image-valid.jpg' }
it "attaches the image to this record" do
expect(share_image.image.metadata["filename"]).to eq filename
end
end
context "with JPG extension and 'text/plain' media type" do
let(:filename) { 'image-with-text-media-type.jpg' }
it "is invalid" do
expect(share_image.errors[:image].to_s).to include("invalid file type")
end
end
# TODO: Refactor the following test (it takes ~50 seconds to run)
context "with a >10mb image file" do
let(:filename) { 'image-11mb.jpg' }
it "is invalid" do
expect(share_image.errors[:image].to_s).to include("too large")
end
end
end
end
I would recommend that you test metadata extraction and validation separately. The metadata extraction you need to test with real IOs, but for validation tests you can assign a cached file with the desired metadata which doesn't have to actually exist.
RSpec.describe ImageUploader do
def uploaded_file(metadata = {})
Shrine.uploaded_file(
"id" => "123",
"storage" => "cache",
"metadata" => {"mime_type" => "image/jpeg", "size" => 100}.merge(metadata)
)
end
let(:share_image) do
FactoryGirl.build(:share_image, image: uploaded_file(metadata).to_json)
end
let(:metadata) { Hash.new }
describe "validations" do
before(:each) { share_image.valid? }
context "when image is correct" do
it "passes" do
expect(share_image.errors).to be_empty
end
end
context "when extension is correct but MIME types isn't" do
let(:metadata) { Hash["filename" => "image.jpg", mime_type => "text/plain"] }
it "fails" do
expect(share_image.errors[:image].to_s).to include("isn't of allowed type")
end
end
context "when file is larger than 10MB" do
let(:metadata) { Hash["size" => 11 * 1024 * 1024] }
it "fails" do
expect(share_image.errors[:image].to_s).to include("too large")
end
end
end
end
Rather than routing uploads through Rack::Test::UploadedFile, create the attachment meta-data record directly using a fixture or factory or directly in the test. The end result should be that you have the attachment meta data (which is what is constructed when you upload a file) that references your file without having to run it through the upload code. I'm not sure about the specifics of doing this with Shrine, but this technique works well with libraries like Paperclip. In Shrine it looks like this will mean constructing a Shrine::UploadedFile record directly that references your file.
In my Rails tests I have this one failing randomly:
require 'spec_helper'
describe Api::V2::ClientsController, type: :controller do
context 'happy path' do
let!(:clients) {
[create(:client), create(:client)]
}
it 'return authorized user resource in JSON format' do
get :codes, format: :json
expect(response).to be_success
expect(json_response['clients'].size).to eql(2)
expect(json_response['clients'][0]).to eql('code' => clients[0].code)
expect(json_response['clients'][1]).to eql('code' => clients[1].code)
end
end
end
I'm not sure about using this:
let!(:clients) {
[create(:client), create(:client)]
}
I assume that your controller fetches the records in no specific order (e.g. Client.all) and that your database doesn't guarantee any default order, either.
In that case, you can use contains_exactly:
expect(json_response['clients'].size).to eql(2)
expect(json_response['clients']).to contain_exactly(
{ 'code' => clients[0].code },
{ 'code' => clients[1].code }
)
You could try swapping out the index numbers (json_response['clients'][0]) for something which doesn't rely on the json returned being in the right order.
For instance:
clients.each do |client|
expect(json_response['clients'].collect{|x|x['code']}).to include(client.code)
end
If this fixes the problem, you may want to order the client records being returned from your controller.
I have the following code in my controller:
format.json { render :json => {
:flashcard => #flashcard,
:lesson => #lesson,
:success => true
}
In my RSpec controller test I want to verify that a certain scenario does receive a success json response so I had the following line:
controller.should_receive(:render).with(hash_including(:success => true))
Although when I run my tests I get the following error:
Failure/Error: controller.should_receive(:render).with(hash_including(:success => false))
(#<AnnoController:0x00000002de0560>).render(hash_including(:success=>false))
expected: 1 time
received: 0 times
Am I checking the response incorrectly?
You could parse the response body like this:
parsed_body = JSON.parse(response.body)
Then you can make your assertions against that parsed content.
parsed_body["foo"].should == "bar"
You can examine the response object and verify that it contains the expected value:
#expected = {
:flashcard => #flashcard,
:lesson => #lesson,
:success => true
}.to_json
get :action # replace with action name / params as necessary
response.body.should == #expected
EDIT
Changing this to a post makes it a bit trickier. Here's a way to handle it:
it "responds with JSON" do
my_model = stub_model(MyModel,:save=>true)
MyModel.stub(:new).with({'these' => 'params'}) { my_model }
post :create, :my_model => {'these' => 'params'}, :format => :json
response.body.should == my_model.to_json
end
Note that mock_model will not respond to to_json, so either stub_model or a real model instance is needed.
Building off of Kevin Trowbridge's answer
response.header['Content-Type'].should include 'application/json'
There's also the json_spec gem, which is worth a look
https://github.com/collectiveidea/json_spec
Simple and easy to way to do this.
# set some variable on success like :success => true in your controller
controller.rb
render :json => {:success => true, :data => data} # on success
spec_controller.rb
parse_json = JSON(response.body)
parse_json["success"].should == true
You can also define a helper function inside spec/support/
module ApiHelpers
def json_body
JSON.parse(response.body)
end
end
RSpec.configure do |config|
config.include ApiHelpers, type: :request
end
and use json_body whenever you need to access the JSON response.
For example, inside your request spec you can use it directly
context 'when the request contains an authentication header' do
it 'should return the user info' do
user = create(:user)
get URL, headers: authenticated_header(user)
expect(response).to have_http_status(:ok)
expect(response.content_type).to eq('application/vnd.api+json')
expect(json_body["data"]["attributes"]["email"]).to eq(user.email)
expect(json_body["data"]["attributes"]["name"]).to eq(user.name)
end
end
Another approach to test just for a JSON response (not that the content within contains an expected value), is to parse the response using ActiveSupport:
ActiveSupport::JSON.decode(response.body).should_not be_nil
If the response is not parsable JSON an exception will be thrown and the test will fail.
You could look into the 'Content-Type' header to see that it is correct?
response.header['Content-Type'].should include 'text/javascript'
When using Rails 5 (currently still in beta), there's a new method, parsed_body on the test response, which will return the response parsed as what the last request was encoded at.
The commit on GitHub: https://github.com/rails/rails/commit/eee3534b
A lot of the above answers are a bit out of date, so this is a quick summary for a more recent version of RSpec (3.8+). This solution raises no warnings from rubocop-rspec and is inline with rspec best practices:
A successful JSON response is identified by two things:
The content type of the response is application/json
The body of the response can be parsed without errors
Assuming that the response object is the anonymous subject of the test, both of the above conditions can be validate using Rspec's built in matchers:
context 'when response is received' do
subject { response }
# check for a successful JSON response
it { is_expected.to have_attributes(content_type: include('application/json')) }
it { is_expected.to have_attributes(body: satisfy { |v| JSON.parse(v) }) }
# validates OP's condition
it { is_expected.to satisfy { |v| JSON.parse(v.body).key?('success') }
it { is_expected.to satisfy { |v| JSON.parse(v.body)['success'] == true }
end
If you're prepared to name your subject then the above tests can be simplified further:
context 'when response is received' do
subject(:response) { response }
it 'responds with a valid content type' do
expect(response.content_type).to include('application/json')
end
it 'responds with a valid json object' do
expect { JSON.parse(response.body) }.not_to raise_error
end
it 'validates OPs condition' do
expect(JSON.parse(response.body, symoblize_names: true))
.to include(success: true)
end
end
JSON comparison solution
Yields a clean but potentially large Diff:
actual = JSON.parse(response.body, symbolize_names: true)
expected = { foo: "bar" }
expect(actual).to eq expected
Example of console output from real data:
expected: {:story=>{:id=>1, :name=>"The Shire"}}
got: {:story=>{:id=>1, :name=>"The Shire", :description=>nil, :body=>nil, :number=>1}}
(compared using ==)
Diff:
## -1,2 +1,2 ##
-:story => {:id=>1, :name=>"The Shire"},
+:story => {:id=>1, :name=>"The Shire", :description=>nil, ...}
(Thanks to comment by #floatingrock)
String comparison solution
If you want an iron-clad solution, you should avoid using parsers which could introduce false positive equality; compare the response body against a string. e.g:
actual = response.body
expected = ({ foo: "bar" }).to_json
expect(actual).to eq expected
But this second solution is less visually friendly as it uses serialized JSON which would include lots of escaped quotation marks.
Custom matcher solution
I tend to write myself a custom matcher that does a much better job of pinpointing at exactly which recursive slot the JSON paths differ. Add the following to your rspec macros:
def expect_response(actual, expected_status, expected_body = nil)
expect(response).to have_http_status(expected_status)
if expected_body
body = JSON.parse(actual.body, symbolize_names: true)
expect_json_eq(body, expected_body)
end
end
def expect_json_eq(actual, expected, path = "")
expect(actual.class).to eq(expected.class), "Type mismatch at path: #{path}"
if expected.class == Hash
expect(actual.keys).to match_array(expected.keys), "Keys mismatch at path: #{path}"
expected.keys.each do |key|
expect_json_eq(actual[key], expected[key], "#{path}/:#{key}")
end
elsif expected.class == Array
expected.each_with_index do |e, index|
expect_json_eq(actual[index], expected[index], "#{path}[#{index}]")
end
else
expect(actual).to eq(expected), "Type #{expected.class} expected #{expected.inspect} but got #{actual.inspect} at path: #{path}"
end
end
Example of usage 1:
expect_response(response, :no_content)
Example of usage 2:
expect_response(response, :ok, {
story: {
id: 1,
name: "Shire Burning",
revisions: [ ... ],
}
})
Example output:
Type String expected "Shire Burning" but got "Shire Burnin" at path: /:story/:name
Another example output to demonstrate a mismatch deep in a nested array:
Type Integer expected 2 but got 1 at path: /:story/:revisions[0]/:version
As you can see, the output tells you EXACTLY where to fix your expected JSON.
If you want to take advantage of the hash diff Rspec provides, it is better to parse the body and compare against a hash. Simplest way I've found:
it 'asserts json body' do
expected_body = {
my: 'json',
hash: 'ok'
}.stringify_keys
expect(JSON.parse(response.body)).to eql(expected_body)
end
I found a customer matcher here: https://raw.github.com/gist/917903/92d7101f643e07896659f84609c117c4c279dfad/have_content_type.rb
Put it in spec/support/matchers/have_content_type.rb and make sure to load stuff from support with something like this in you spec/spec_helper.rb
Dir[Rails.root.join('spec/support/**/*.rb')].each {|f| require f}
Here is the code itself, just in case it disappeared from the given link.
RSpec::Matchers.define :have_content_type do |content_type|
CONTENT_HEADER_MATCHER = /^(.*?)(?:; charset=(.*))?$/
chain :with_charset do |charset|
#charset = charset
end
match do |response|
_, content, charset = *content_type_header.match(CONTENT_HEADER_MATCHER).to_a
if #charset
#charset == charset && content == content_type
else
content == content_type
end
end
failure_message_for_should do |response|
if #charset
"Content type #{content_type_header.inspect} should match #{content_type.inspect} with charset #{#charset}"
else
"Content type #{content_type_header.inspect} should match #{content_type.inspect}"
end
end
failure_message_for_should_not do |model|
if #charset
"Content type #{content_type_header.inspect} should not match #{content_type.inspect} with charset #{#charset}"
else
"Content type #{content_type_header.inspect} should not match #{content_type.inspect}"
end
end
def content_type_header
response.headers['Content-Type']
end
end
For Your JSON response you should parse that response for expected results
For Instance: parsed_response = JSON.parse(response.body)
You can check other variables which is included in response like
expect(parsed_response["success"]).to eq(true)
expect(parsed_response["flashcard"]).to eq("flashcard expected value")
expect(parsed_response["lesson"]).to eq("lesson expected value")
expect(subject["status_code"]).to eq(201)
I prefer also check keys of JSON response, For Example:
expect(body_as_json.keys).to match_array(["success", "lesson","status_code", "flashcard"])
Here, We can use should matchers For expected results in Rspec