How to verify params[:][:] value by "assigns" in rspec controller - ruby-on-rails-3

Following is my controller code:
def create
params[:order][:user_id]=current_user.id
params[:order][:company_id]=current_company.id
:
:
end
In my rspec controller test i want to test create method so in that i have to test for
assigns(params[:order][:user_id]).should eq(user)
assigns(params[:order][:company_id]).should eq(compnay)
user and company i set in let and stub as current_user,current_compnay so i get the value for current_user.id andcurrent_company.id . My issue is how can i verify params variable in which we provide value in controller.
currently above rspec lines not working. can anyone help me to resolve above issue?

should be:
controller.params[:order][:user_id].should eq(user.id)
controller.params[:order][:company_id].should eq(company.id)

Related

Bad override class name for this key. Please update class_name field

I'm using prestashop 1.4
I get the following error in the webservice api
Bad override class name for this key. Please update class_name field
This didn't happened before on similar installation.
I notice the line:
if ($bad_class_name) {
$this->setError(500, 'Bad override class name for this key. Please update class_name field', 126);
}
when I comment this line, it seems to work (though the full loop still fails).
The bad_class_name is passed to the fetch method.
but why?
this is a result of wrong username (user key)
in chrome -
add username# at the begining of the url to relogin - e.g. username#localhost/api

cakephp3.0 Authentication

Hey In authentication of cakephp3.0 I am facing some problem Hey how can i create an authentication based on patient table and patient controller like url/patient/login I have controller and model and form ready but when I go to login page it always search for query from usertable by default when I am trying to add this code in Patientcontroller :
$this->Auth->config('authenticate', [
AuthComponent::ALL => ['userModel' => 'Members'],
'Basic',
'Form']);
I am getting this error :
Error: Class 'App\Controller\AuthComponent' not found
File D:\xampp\htdocs\hwapp\src\Controller\PatientController.php
Line: 34``
Please read this section of the documentation: http://book.cakephp.org/3.0/en/controllers/components/authentication.html#configuring-authentication-handlers
It explains how to use other table for handling login and how to tell the AuthComponent to use anther controller and action to handle the logic.

Parsing request.request_parameters in Rails

I'm building a web service app and I'm trying to handle nicely a 422 page sending back to the user the JSON the POSTed to better debug the error. To do this, I use request.request_parameters which get me back the JSON I sent, but it happens to be organized (for me) in a wired way and I can't really get it back only with the original data
What I send as JSON is this.
{
"name":"New set intensity",
"properties":
[
{"uri":null,"value":"on"},
{"uri":"https://type.lelylan.com/properties/intensity","value":"100.0"}
]
}
What I get from request.request_parameters is this.
{"{\"name\":\"New set intensity\",\"properties\":"=>{"{\"uri\":null,\"value\":\"on\"}, {\"uri\":\"https://type.lelylan.com/properties/intensity\",\"value\":\"100.0\"}"=>{"}"=>nil}}}
My main problem is that somehow the content becomes the key, and this recursively inside. Is there a way to get back the clean data? Thanks a lot.
UPDATE: I'm trying to better understand where and why this problem occurs.
In my controller I tried to access in the two available ways I know.
# request.body.read.inspect
"{\"name\":\"New set intensity\",\"properties\":[{\"uri\":\"not_valid\"}]}"
# request.request_parameters
{"{\"name\":\"New set intensity\",\"properties\":"=>{"{\"uri\":\"not_valid\"}"=>{"}"=>nil}}}
The request is made from Capybara
page.driver.post(#uri, #params.to_json)
The controller returns only JSON so this is the way I defined it. I din't put 'respond_to' and 'respond_with' and when I make the request it renders the json view show.rabl.json. This makes me think that it recognize the correct format.
class FunctionsController < ApplicationController
before_filter
...
def index
...
end
def show
..
end
def create
body = JSON.parse(request.body.read)
#function = Function.new(body)
if #function.save
render 'show', status: 201, location: FunctionDecorator.decorate(#function).uri
else
render_422 "notifications.resource.not_valid", #function.errors
end
end
Thanks.

How to display JSON data using a Ruby (Rails) method

I'm using the RapLeaf API in a Rails app and when I call a RapLeaf record I get a response in JSON. How can I display JSON data using a Ruby method?
Here is the controller:
#user = User.find_by_id(params[:id])
#api = RapleafApi::Api.new('<MY_SECRET_API_KEY>')
Here is the view:
%td
= #api.query_by_email(#user.email)
Here is what it looks like when rendered in the page:
{"gender"=>"Male"}
I want to be able to call the gender this way
%td
= #api.query_by_email(#user.email).gender
But I get an undefined method `gender' for {"gender"=>"Male"}:Hash
What sort of method do I need to create to call gender using .gender?
Turns out that output data is a Ruby hash and not JSON. I've got to get my terminology strait.

How to test a search controller action which has filters in Rails with RSpec?

We have a bar with filters in almost all of our table-based views in a rails app and we need to test the controller action.
An example of the code:
def index
#users = User.
with_status(params[:status]).
with_role(params[:role_id]).
search(params[:q])
end
The above methods are ActiveRecord scopes which are setup to be bypassed if the passed value if blank.
What I need to do now is spec it sanely and test all the esge cases:
no params passed
only role, only status, only search
role + status, role + search, ... (pairs of 2)
role + status + search
The basic spec example I have written is as follows:
context "when filtering by status" do
before do
1.times { Factory(:user, :status => "one") }
3.times { Factory(:user, :status => "other") }
end
it "returns only users with the provided :status" do
get :index, :status => "one"
assigns(:users).size.should == 1
assigns(:users)[0].status.should == "one"
end
end
I want to write a matrix that will mix and match the role, status and search params and generate the appropriate spec examples.
Is the Array#permutation the solution or is there a better way to do it?
I would test the scopes in the model, so make sure that they can handle the blank value correctly, and also handle the set value correctly.
Then inside the controller, I would test the expectation that the chain is called (use stub_chain). The fact that the chain will return the correct result is handled by the fact that each scope individually has the correct behaviour (you tested that), and the combined behaviour is ensured by rails/activerecord. You should test the passed parameters are handled correctly.
The code you built to test the matrix is very impressive. But for me I try to make sure that my tests are readable, I consider them a kind of documentation of what a piece code is expected to do. To me your code is not comprehensible at first sight.
Hope this helps.