I'm trying to use expect_to_receive rspec expectation but without any luck. My controller specs file looks like that:
it 'resolves an end behaviour' do
expect_any_instance_of(Job).to receive(:resolve_end_behaviour)
post :callback, #params
end
And my controller method:
def callback
#video = JobVideo.find(params['body']['id'].to_i)
if !#video.is_ready
#video.job.resolve_end_behaviour
#video.update_attribute(:is_ready, true)
end
render json: { success: true }
end
And running rspec gives me:
Exactly one instance should have received the following message(s) but didn't: resolve_end_behaviour
I'm sure that this method is called cause is_ready column is beeing updated. Can anyone give me any clues on why is that happening and what can i do about it? Thanks in advance.
Env:
rails 4
rspec 3
Your test is expecting resolve_end_behaviour to be called with two arguments, you are calling it with no arguments.
Related
I'm trying to get a variable from the controller over to the model. Its the value of a checkbox passed up from the View. I cant see it in the controller (0 or 1) and just need to get it to the model. I have seen tons of examples on here but none of them seem to work for me. Maybe its something about the Ruby environment we have setup? Its a Rails 5 environment.
Here is what I have tried so far and the errors it gives in the logs:
Controller:
#vin = Vehicle.create!
#results = #vin.myvin!(params[:vehicle][:vincheck])
Model:
def myvin!(localvin)
logger.info "MADE IT INTO VIN"
end
Error in development.log:
AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action.
I'm not calling render or redirect!! Just trying to call the method in the model.
I have also tried a variation like:
Controller:
Vehicle.myvin(params[:vehicle][:vincheck])
Model:
def self.myvin(localvin)
logger.info "MADE IT INTO VIN"
end
But regardless... I always get that render error in the logs!
I have tried instantiating a new object:
Controller:
Vehicle.new(params[:vehicle][:vincheck])
Model:
attr_reader :localvin
def initialize(localvin)
logger.info "MADE IT INTO VIN"
end
When I do that I get:
500wrong number of arguments (given 0, expected 1)
Short Stack
app/models/vehicle.rb:25:in 'initialize'
app/controllers/vehicles_controller.rb:84:in 'new'
Anyone have any thoughts on how I can get this data over? Maybe it has something to do with the Vehicle Model itself? In the controller it gets instantiated at different places.
For example:
def index
#vehicles = Vehicle.active
end
def create
#this is where I am putting all my code
#not really sure how this is getting created since there was no
#initialize in the code before I added one
#vehicle = Vehicle.new(vehicle_params.merge(
created_by_id: current_user.id,
created_by_name: current_user.name,
deleted: false))
end
thanks!
Using Rails 4.2, Minitest, and Shoulda...
student_phone_numbers_controller_test.rb
class StudentPhoneNumbersControllerTest < ActionController::TestCase
context "deleting to remove" do
context "when the phone number doesn't belong to the student" do
setup do
log_in_as(teachers(:schmidt))
delete :remove,
format: "js",
student_id: students(:two).id,
id: phone_numbers(:one_one).id
end
should use_before_action :logged_in_teacher
should use_before_action :set_student
should respond_with :redirect
should redirect_to { student_path students(:two) }
end
end
end
New to Shoulda and Rails testing in general, and my code seems to parallel the Minitest documentation for redirect_to. However, I'm banging into this error when running the test...
$ rake test:controllers
rake aborted!
ArgumentError: wrong number of arguments (0 for 1)
...pointing at the should redirect_to line. I assume it wants the controller/action hash?
The following line works as expected:
should redirect_to(controller: :students, action: :show) { student_url students(:two) }
...as does this...
should redirect_to({}) { student_url students(:two) }
The first fix is totally redundant. The second seems superfluous. I realize the source for this method doesn't have a default value for the single argument. Should it?
Or, am I missing something otherwise obviously about the purpose/workings of this helper?
This is actually a bug in the documentation and in fact was raised as an issue just recently: https://github.com/thoughtbot/shoulda-matchers/issues/788. The first argument is meant to be a human-readable representation of the route; it's inserted into the name of the test and thus will show up in the output when you run the test.
So you might have something like:
should redirect_to("the URL for student two") { student_url students(:two) }
Hope this helps.
I'm working on a rails app that serves some json and I'm having hard time understanding what is going on in the code below (simplified for the purpose of understanding the issue).
module Api
class ProjectController < ApplicationController
respond_to :json
def show
x = {"id"=>17, "name"=>"abc", "version"=>1}
respond_with x.to_json, status: 200
end
def create
x = {"id"=>17, "name"=>"abc", "version"=>1}
respond_with x.to_json, status: 200
end
end
end
The show action works fine but when I call the create action I get
NoMethodError (undefined method '{"id":17,"name":"abc","version":1}_url' for
Api::ProjectsController:0x007fbb2294cd18)
Why do I get this error while show works just fine? is it because create makes a post instead of a get?
How can I solve it?
Thanks for your help and have a nice day.
The issue is that your controller is in a module (Api). That affects the namespace, and thus the routing; you must include that namespace as part of what you pass to respond_with.
Normally, I'd direct you to this stackoverflow answer (credit goes there). But since you are using a hash instead of a model, this answer might be more applicable.
Note how Rails is trying to call a ..._url method. The ... is your hash, in JSON format. You need to help Rails here on how to render.
I'm trying to write spec for inherited_resources controller. I decided to mock all integration with the database using rspec's mock_model. Unfortunately I can't write spec for create and update action because I'm getting the following error: https://gist.github.com/936947
Can someone help me with this issue?
I was having the same issue using flexmock.
the cause is that it does not use the update_attributes method to make the routing decision. It checks the resource.errors to see whether it is empty.
So to get it to respond properly, we will need to mock out the errors method as well.
Here is the pertinent code #line 248 in lib/inherited_resources/base_helpers.rb
def respond_with_dual_blocks(object, options, &block) #:nodoc:
args = (with_chain(object) << options)
case block.try(:arity)
when 2
respond_with(*args) do |responder|
blank_slate = InheritedResources::BlankSlate.new
if object.errors.empty?
block.call(responder, blank_slate)
else
block.call(blank_slate, responder)
end
end
when 1
respond_with(*args, &block)
else
options[:location] = block.call if block
respond_with(*args)
end
end
The failure messages are about the inability to access named routes from inside the controller, so I'm not sure that this has anything to do with mock_model. Have you tried the same examples using real models?
I am trying to write an rspec test for a controller that accesses a
model Group.
#request.env['HTTP_REFERER'] = group_url(#mock_group) ### Line 49
I get this:
NoMethodError in 'ActsController responding to create should redirect to :back'
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.rewrite
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:621:in `url_for'
(eval):17:in `group_url'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/test_process.rb:464:in `send!'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/test_process.rb:464:in `method_missing'
This line in url_for is the problem; specfically #url is nil.
#url.rewrite(rewrite_options(options))
And it seems that #url is initialized here:
def initialize_current_url
#url = UrlRewriter.new(request, params.clone)
end
This happens because url_for depends on stuff that's initialized during request processing. I assume your test looks something like this:
it "should do whatever when referrer is group thing" do
#request.env["HTTP_REFERER"] = url_for(#mock_group)
get :some_action
"something".should == "something"
end
url_for fails because it happens before the get. The easiest way to resolve the problem is to hard-code the URL in your test (i.e. change url\_for(#mock\_group) to "http://test.host/group/1"). The other option is to figure out how to get #controller to initialize #url before you call url_for. I think I've done that before, but I don't have the code around any more and it involved digging through action_controller's code.
Look at this. I think it is relevant.
http://jakescruggs.blogspot.com/2008/11/if-you-use-mocha-and-rspec-then-read.html