Request in Rails Testing not returning body - ruby-on-rails

I'm starting to do some testing to my app in rails, and following official tutorial, I've written this:
class UserFlowTest < ActionDispatch::IntegrationTest
def login
post ns_login_user_path, { :user => { :username => 'user', :password => 'password' } }
assert_response 200
end
test "should complete a flow" do
login
post create_participant_path(:event_id => events(:myevent).id), {
:format => :json,
:event_role => event_roles(:regular_participant).id,
:in_team => true
}
r = JSON.parse(response.body)
assert_response 200
puts "response creating participation #{r.as_json}"
participant_id = r[:participant_id]
end
end
It does the login OK, but after that, when trying to create the participant, response is a variable with no .body attribute, just the number 200 (the status), so the JSON.parse method crashes.
This is the relevant part of my routes.rb:
# Events
scope 'events', :controller => :events do
# some routes
scope ':event_id', :controller => :events do
# some routes
scope 'participants', :controller => :participants do
post '', :action => :create_participant, :as => :create_participant
# some routes
end
end
end
And the controller ParticipantsController.rb:
class ParticipantsController < ApiController
before_action :require_login, :only => [:create_participant, :update_participant]
# Creates a participation of a person in the event
# Receives the following params:
# - +event_id+
# - +in_team+::_boolean
# - +event_role+
def create_participant
# … some logic
if participant.save
render :status => :ok, :json => Hash[
:participant_id => participant.id,
:team_participant_id => participant.team_participant_id
]
else
render :status => 406, :json => Hash[
:message => t('alerts.error_saving'),
:errors => participant.errors.as_json
]
end
end
end

I've seen the full response object on controller specs, but looking at the code, it appears that the ActionDispatch::IntegrationTest code only returns the response.status, not the entire response object.
The documentation doesn't directly say that you can access the response object. You might try doing a render_views and see if that makes any difference, but based on inspection of the code, it doesn't seem like it will.

Related

Rspec Test for JSON post Rails

Controller: payments_controller.rb
class PaymentsController < ApplicationController
# This is needed to have Postman work
skip_before_action :verify_authenticity_token
rescue_from ActiveRecord::RecordNotFound do |exception|
render json: 'not_found', status: :not_found
def create
new_payment = Payment.new(new_params)
current_loan = Loan.find(new_params[:loan_id])
if Payment.valid?(new_payment, current_loan)
Payment.received(new_payment, current_loan)
current_loan.save
new_payment.save
redirect_to '/loans'
else
raise 'Amount entered is above the remaining balance'
end
end
end
This method works when I test it in Postman. However, I can't seem to write a test for it that passes. I currently have:
payments_controller_spec.rb
require 'rails_helper'
RSpec.describe PaymentsController, type: :controller do
describe "#create", :type => :request do
let!(:loan) {Loan.create!(id: 1, funded_amount: 500.0)}
params = '{"payment":{"amount":400, "loan_id":2}}'
it 'creates and saves a payment while saving the associated fund_amount of the loan' do
post "/payments", params.to_json, {'CONTENT_TYPE' => 'application/json', 'ACCEPT' => 'application/json'}
expect(loan.funded_amount).to eql(600.0)
end
end
end
The error is:
Failure/Error: post "/payments", params.to_json, {'CONTENT_TYPE' => 'application/json', 'ACCEPT' => 'application/json'}
ActionController::ParameterMissing:
param is missing or the value is empty: payment
Valid parameters (that work with Postman) are:
{"payment":{"amount":400,"loan_id":2}}
Any help would be appreciated!
*** UPDATE ****
After messing around with this for a while, I finally got it to work with this:
describe "#create", :type => :request do
let!(:loan) {Loan.create!(id: 1, funded_amount: 500.0)}
it 'creates and saves a payment while saving the associated fund_amount of the loan' do
json = { :format => 'json', :payment => { :amount => 200.0, :loan_id => 1 } }
post '/payments', json
loan.reload
expect(loan.funded_amount).to eql(300.0)
end
end
You can pass in your params like this.
it 'creates and saves a payment while saving the associated fund_amount of the loan' do
post "/payments", payment: { amount: 400, loan_id: 1 }, {'CONTENT_TYPE' => 'application/json', 'ACCEPT' => 'application/json'}
expect(loan.funded_amount).to eql(600.0)
end

trying to do a query session for Parse

using the gem parse-ruby-client and im trying to do a query session, here is my code.
im just curious on how to implement most of the https://parse.com/docs/rest/guide. Also i dont know if i created a session to begin with? i think the gem does it automatically? not too sure
class LoginController < ApplicationController
def index
end
def log_in
user = Parse::User.authenticate(params[:user][:username], params[:user][:password])
#username = params[:user][:username]
rescue Parse::ParseProtocolError
redirect_to :controller => "login"
end
def logout
Parse.client.post("https://api.parse.com/1/logout", {})
rescue Parse::ParseProtocolError
redirect_to :controller => "login"
end
def query_session
Parse.client.post("https://api.parse.com/1/users/", {})
end
end
here is log_in.html.erb
<h4>You are logged in as: <u><%= #username %></u></h4>
<%= link_to "logout", :controller => "login", :action => 'logout' %><br>
<%= link_to "test", :controller => "login", :action => 'query_session' %>
routes.rb
Rails.application.routes.draw do
root 'login#index'
get 'login/log_in' => 'login#log_in'
post 'login/log_in' => 'login#log_in'
get 'login/logout'
get 'login/query_session' => 'login#query_session'
end
this is my parse.rb
require 'parse-ruby-client'
Parselogin::Application.configure do
config.parse = Parse.init :application_id => 'APIKEY',
:api_key => 'APIKEY'
end
i get back this error when i click on 'test' button from the log_in.html.erb
201: missing user password
You are missing the initialization of the Parse.client. This is an example from the gem's official documentation.
require 'parse-ruby-client'
client = Parse.create :application_id => '<your_app_id>',
:api_key => '<your_api_key>',
:quiet => true | false
To use the client later on, do the actions like this.
client.post("https://api.parse.com/1/logout", {}), where client is the variable from 1st snippet.
In order to respond to the second part of your question, please consider the gem's documentation.

RSpec test failing for routes that accept id param

I'm trying to test the ArticlesController in my Rails applications. All of the routes that do not accept params are passing. But all of the routes that expect an id param are failing.
Failures:
1) ArticlesController should find article by id
Failure/Error: get :info, id: #article[:id]
ActionController::UrlGenerationError:
No route matches {:action=>"info", :controller=>"articles", :id=>"60"}
# ./spec/controllers/articles_controller_spec.rb:26:in `block (2 levels) in <top (required)>'
2) ArticlesController should export folder
Failure/Error: get :export_folder, id: #article[:id]
ActionController::UrlGenerationError:
No route matches {:action=>"export_folder", :controller=>"articles", :id=>"60"}
# ./spec/controllers/articles_controller_spec.rb:56:in `block (2 levels) in <top (required)>'
3) ArticlesController should export an article by id
Failure/Error: get :export, id: #article[:id]
ActionController::UrlGenerationError:
No route matches {:action=>"export", :controller=>"articles", :id=>"60"}
# ./spec/controllers/articles_controller_spec.rb:50:in `block (2 levels) in <top (required)>'
config/routes.rb
get '/articles/list' => 'articles#list', defaults: { format: :html }
get '/articles/trendlist' => 'articles#trendlist', defaults: { format: :html }
get '/articles/show/:id' => 'articles#show', defaults: { format: :html }, as: :show_article
get '/articles/index'
get '/articles/info/:id' => 'articles#info', as: :article_info
get '/articles/export/:id' => 'articles#export', as: :export_article
get '/articles/view/:id' => 'articles#view'
get '/articles/favorite/:id' => 'articles#favorite'
get '/articles/trending' => 'articles#trending', defaults: { format: :json }
get '/articles/deleted' => 'articles#deleted', defaults: { format: :json }
get '/articles/csv/:id' => 'articles#csv'
get '/articles/export_folder/:id' => 'articles#export_folder', as: :export_folder
spec/controllers/articles_controller.rb
require 'spec_helper'
describe ArticlesController do
before(:all) do
Article.destroy_all
Comfy::Cms::Layout.destroy_all
Comfy::Cms::Site.destroy_all
site = FactoryGirl.create(:cms_site)
layout = FactoryGirl.create(:cms_layout, site_id: site[:id])
#article = FactoryGirl.create(:cms_page, layout_id: layout[:id], site_id: site[:id])
end
it 'should index articles' do
get :index
expect(response.response_code).to eq(200)
expect(response.headers).to include( 'Content-Type' => 'application/json; charset=utf-8')
end
its 'should list articles' do
get :list
expect(response.response_code).to eq(200)
expect(response.headers).to include( 'Content-Type' => 'text/html; charset=utf-8')
end
it 'should find article by id' do
get :info, id: #article[:id]
expect(response.response_code).to eq(200)
expect(response.headers).to include( 'Content-Type' => 'application/json; charset=utf-8')
end
it 'should list deleted articles' do
get :deleted
expect(response.response_code).to eq(200)
expect(response.headers).to include( 'Content-Type' => 'application/json; charset=utf-8')
end
it 'should list trending articles' do
get :trending
expect(response.response_code).to eq(200)
expect(response.headers).to include( 'Content-Type' => 'application/json; charset=utf-8')
end
it 'should update trending articles' do
get :trendlist
expect(response.response_code).to eq(200)
expect(response.headers).to include( 'Content-Type' => 'text/html; charset=utf-8')
end
it 'should export an article by id' do
get :export, id: #article[:id]
expect(response.response_code).to eq(200)
expect(response.headers).to include( 'Content-Type' => 'text/csv; charset=utf-8')
end
it 'should export folder' do
get :export_folder, id: #article[:id]
response.response_code.should eq(200)
expect(response.headers).to include( 'Content-Type' => 'text/html; charset=utf-8')
end
end
rake routes
Prefix Verb URI Pattern Controller#Action
tags GET /tags(.:format) tags#index
articles_list GET /articles/list(.:format) articles#list
articles_trendlist GET /articles/trendlist(.:format) articles#trendlist
articles GET /articles/show/:id(.:format) articles/articles#show
articles_index GET /articles/index(.:format) articles#index
GET /articles/info/:id(.:format) articles/articles#info
GET /articles/export/:id(.:format) articles/articles#export
GET /articles/view/:id(.:format) articles/articles#view
GET /articles/favorite/:id(.:format) articles/articles#favorite
articles_trending GET /articles/trending(.:format) articles#trending
articles_deleted GET /articles/deleted(.:format) articles#deleted
GET /articles/csv/:id(.:format) articles/articles#csv
GET /articles/export_folder/:id(.:format) articles/articles#export_folder
app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
include ArticlesHelper
before_action :set_default_response_format, except: [:pdf, :show, :list, :trendlist, :export_folder]
def index
#articles = SearchArticlesCommand.new(params).execute
end
def deleted
#dlist = Article.deleted.map(&:article_id)
render :ids, status: :ok
end
def info
id = params[:id].to_i
#article = Article.published.find_by(id: id)
end
def list
#articles = Article.folder
render 'articles/list'
end
def favorite
...
render json: { result: true, is_liked: "#{is_liked}" }
end
def view
...
render json: { result: true }
end
def trending
load_trending_articles
end
def trendlist
load_trending_articles
render 'articles/trendlist'
end
def export
id = params[:id].to_i
#article = Article.published.find_by(id: id)
render pdf: #article.label.gsub(/\s/, '_'),
template: 'articles/export.pdf.erb',
dispostion: 'attachment',
locals: { paragraphs: #article.paragraphs, images: #article.images }
That is not really what namespace is used for. You can read up more on it here. Use resources instead and specify member for the one with id:
resources :articles, only: [] do
collection do
get :list
get :trendlist
get :trending
get :deleted
end
member do
get :info
get :export
get :view
get :favorite
get :csv
get :export_folder
end
end
get 'articles/index', to: 'articles#index'
get 'articles/show/:id', to: 'articles#show'
If you look at the output of rake routes you can see that Rails is looking for articles/articles#show etc. namespace is for creating routes which live in a namespace (duh) such as /admin/tools which would root to Admin::ToolsController.
You can instead use scope which adds a url prefix but not the namespace or resources:
resources :articles, only: [:show, :index] do
member do
get 'info'
get 'export' # Use /articles/1.format instead.
get 'view' # Do you need this? Code smell!
get 'favorite' # should be post - GET should never create or alter a resource.
get 'csv' # remove - use /articles/1.csv instead
get 'show' # /articles/show/3
end
collection do
get 'trending'
get 'deleted'
get 'trendlist'
get 'list' # Do you need this? Code smell!
get 'index' # /articles/index
end
end
I would also question why you actually need so many routes beyond the standard CRUD set.
Especially routes which are extremely in semantics like view and show and list and index.
I would use query parameters around a smaller set of routes as it reduces the amount of duplication on all levels.
/articles?filter=deleted => index
/articles?filter=trending
Rails also has a built in CSV mime type so you can do:
/articles/5.csv
class ProductsController < ApplicationController
def show
#article = Article.order(:name)
respond_to do |format|
format.html
format.csv { render text: #article.to_csv }
end
end
end
Using #article[:id] vs #article.id does work but its unidiomatic and very slightly slower since rails has to go through the [] method just to find the getter method. Its not a huge deal in this case but not great when you are dealing with a large number of objects.

Rspec 2.10.1, Rails 3.2.13 - ActionController::RoutingError: No route matches

I'm writing an rspec test that sends a JSON via POST to a member route end point. But I'm getting a "no route matches" error when I do so. I'm not sure if there's something I need to add because this endpoint is a member route, or I'm just missing some HTTP request headers because I'm sending a JSON. Please help.
This is what I have:
Spec:
describe "#endpoint" do
context "type 1" do
before(:each) do
post :create, #params.merge(:abc => {:first_user_id => #user1.id, :second_user_id => #user2.id})
#mashup = assigns(:mashup)
end
it "should post the results successfully" do
units = [...]
users = [...]
params = #params.merge(:mashup_outcome => {:status => "success", :assetName => "MashupAssetName", :winningUserId => #user1.id}, :mashup_id => #mashup.id, :version_number => 1, :user_id => #user1.id, :id => #mashup.id ,:users => :users).to_json
#request.env["CONTENT_TYPE"] = "application/json"
#had to have a param key for my params below in order to bypass the NoMethodError
#In actual request body, it's just a JSON
post :over, :mashup => params
#mashups.in_progress.should be_false
end
end
context "type 2" do
before(:each) do
...
end
it "should post the results correctly" do
...
end
end
Routes:
namespace :mashup do
resources :mashups do
member do
post :endpoint
end
end
Controller:
def endpoint
if #mashup.complete_mashup(params)
render :json => api_success(#mashup.dpoints)
else
render :json => api_error({})
end
end
Error:
Failure/Error: post :endpoint, :mashup => params
ActionController::RoutingError:
No route matches {:mashup => "{...<JSON>...}", :controller => "api/mashup/mashups", :action => "endpoint"}

RSPEC how to post to a controller? What's wrong with this?

I'm trying to post to my controller in RSPEC, see anything wrong with this? It's failing w/o error:
it "should store create an IncomingMail record" do
lambda {
post 'create', {
"from" => 'XXX',
"to" => 'XXX',
"cc" => 'XXX',
"subject" => 'XXX',
"message_text" => 'XXX',
"message_html" => 'XXX' }
}.should change { IncomingMail.count }.by(1)
end
Updated:
it "should store create an IncomingMail record" do
post :create,
:from => 'xx',
:to => 'xx',
:cc => 'xx',
:subject => 'xx',
:message_text => 'xx',
:message_html => 'xx'
mail = IncomingMail.last(:order => 'created_at desc')
mail.from.should == 'xx'
end
Controller
class IncomingMailsController < ApplicationController
require 'iconv'
#make sure that rails doesn't raise an exception because we have no way of knowing the token
skip_before_filter :verify_authenticity_token
def create
begin
#incoming_mail = IncomingMail.create(
:from => params[:from],
:to => params[:to],
:cc => params[:cc],
:subject => params[:subject],
:message_text => message_text_utf8,
:message_html => message_html_utf8
)
.....
This is how i do it :
Route Example :
post 'train_ability/:ability' => :train_ability, :as => 'train_ability'
Spec :
it "should increase the strength ability by one point and also update the strength_points by one if strength is the trained ability" do
#user.str = 10
#user.str_points = 0
#user.save!
post :train_ability, :ability => 'str'
#user.reload
flash[:error].should be_nil
#user.str_points.should == 1
#user.str.should == 11
end

Resources