Sidekiq testnig in rspec gave wrong argument error - ruby-on-rails

when i try to test the rspec for the sidekiq, it sending the params as string to the method.
for example:
my rspec
" expect(User::RemoveAccess).to have_enqueued_sidekiq_job(user.id,user_account_ids)
"
here user account ids is an array
like user_account_ids = user.pluck(:account_ids)
my ruby code:
User::RemoveAccess.perform_async(user.id,user.pluck(:account_ids))
the error I'm getting in rspec is:
expected to have an enqueued User::RemoveAccess job
arguments: [12670, ["e012-40d-4f50-8d96-14"]]
found
arguments: [[12670, "e012-40d-4f50-8d96-14"]]
I'm not sure where I'm getting wrong, is any thing i missed?.

The wrong arguments error is happening because you are calling .pluck on a single user instance and not an active record collection
If you want an array, you need to change this
user.pluck(:account_ids)
To this
users.pluck(:account_ids)

Related

RSpec error: 'but it does not respond to `include?`'

I am running the following rspec test
expect(City.first.city_openings('2014-05-01', '2014-05-05')).to include(#listing2,#listing3)
I am getting this result
This is confusing to me because it seems that my result does in fact include the right values. But rspec says that my result "does not respond to include?" Any ideas on how to fix this? I don't see much about it anywhere.
Your method city_openings return true and not a array.
TrueClass is not iterable with include? method.
See the documentation here
https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/include-matcher
https://relishapp.com/rspec/rspec-expectations/v/3-7/docs/built-in-matchers/match-matcher

RSpec stub throws wrong number of arguments error

I'm try to stub the Slack gem with the new syntax (old syntax throws the same error):
before :each do
allow(Slack).to receive(:channels_join).and_return(true)
end
This line throws wrong number of arguments (2 for 1). Breaking up the line into pieces, it seems like the call to .to is throwing the error:
a = allow(Slack)
b = receive(:channels_join)
c = b.and_return(true)
a.to(c)
Changing the arguments to .to didn't change anything:
a.to(c, c)
throws the same 2 for 1 error.
a.to(5)
throws a reasonable error: only the receive or receive_messages matchers are supported with allow(...).to, but you have provided: 5
Why is the 2 for 1 error being thrown?
If you have enabled the verify_partial_doubles option than RSpec will check if Slack responds to :channels_join when you stub it. Unfortunately, Slack.respond_to? is implemented incorrectly:
def self.respond_to?(method)
return client.respond_to?(method) || super
end
The problem is that Object#respond_to? accepts two arguments (and has for years, since at least 1.8.7, if not sooner!), and RSpec passes a 2nd arg, expecting that respond_to? will accept two arguments since it is supposed to.
To fix it, you can (temporarily) monkey patch the slack gem:
module Slack
def self.respond_to?(method, include_all=false)
return client.respond_to?(method, include_all) || super
end
end
This should really be fixed in the slack gem, though, so I'd encourage you to open a PR with the maintainers with this fix.
More broadly, when you run into this kind of error, you can learn more about the problem by passing rspec the -b (or --backtrace) flag, which will print the full backtrace. In your situation I would have expected it to show both the respond_to? call site within RSpec and the line where Slack defines respond_to? accepting only one argument. Then you could look at those lines to figure out what's going on.

Rails- upgrading to ruby 2.2.2 - no implicit conversion of Array into String (TypeError)

very new to ruby and rails.. Ive been working on a project, that simply reads in files and parses them to store into a database. Project was running fine, however running the code after an update to ruby 2.2.2 , I received an error that wasn't previously there:
in `foreach': no implicit conversion of Array into String (TypeError)
Here is the snippet of the foreach thats causing an error: (let me know if more code is necessary).
def parse(file_name)
File.foreach(file_name).with_index do |line, line_num|
puts "\nLine #{line_num}: #{line}"
Does anyone know whats going on?
Thank you
EDIT: Sorry it was a snippet! Im calling this ruby code into my rails Test called "parse_log_file_test"
require File.dirname(__FILE__) + '/../test_helper'
class ParseLogFileTest < ActiveSupport::TestCase
filename = Array.new
Dir.glob('database/oag-logs/*.log').each do |log_file|
filename.push(log_file)
end
parser = ParseLogFile.new
parser.parse(filename)
test 'parse' do
parser = ParseLogFile.new
filename.each do |log_file|
begin
parser.parse(log_file)
rescue
puts"ERROR: Unable to parse line #{log_file}"
end
end
assert true
end
end
I'm guessing you omitted the end to your function, but if you don't have it, you need it.
This error indicates that the argument passed to parse as file_name is an array instead of a string.
However, if that's the case, it fails the same on e.g. Ruby 1.8.4:
File.foreach([]).with_index do |line, line_num|
puts "\nLine #{line_num}: #{line}"
end
Output:
TypeError: can't convert Array into String
from (irb):1:in `foreach'
from (irb):1:in `with_index'
from (irb):1
from :0
Thus my guess is that the code that produces the value you pass to parse returned a string in your previous Ruby version and returns an array in 2.2.2.
In your case, the error is caused by the first invocation of parser.parse(...), right above the test 'parse' do line, not by the invocation inside the test method. I guess you put that invocation there after the migration, probably to debug a problem. But you are passing a different argument to the first invocation than to the invocation inside the test method, so it fails for a different reason than the one in the test method.
To see what Error is caused inside your test, simply remove the lines
rescue
puts"ERROR: Unable to parse line #{log_file}"
(Keep the end or you'll have to remove the begin, too.)
This way, an Error will hit the test runner, which will usually display it including message and stack trace.
Agreed with the poster above that you are most likely missing quotation marks. It should have nothing to do with 2.2.2 though, probably you are copy-pastying your file name differently this time around.
So apparently this is an issue with upgrading to ruby 2.2.2 on windows. After getting past this error, I only encountered more errors.. nokogiri..etc
I have recently got a mac and the errors went away.

send object from application_rake to a model

I have a task in application_rake. The lines that matter are:
family = Family.find(114)
SysMailer.deliver_feedback_memo(emails_str,"escorter", family)
In the SysMailer model i try to write the name of the family like this:
puts family.name
name is an attribute of family (for sure!!!)
But when I run:
rake the_task
I get an error:
rake aborted!
missing attribute: name
Why? is there a problem to send an object from rake to a model?
UPDATE:
After struggling with the issue, I tried not to pass the object, but pass only the family.id to the model like this:
SysMailer.deliver_feedback_memo(emails_str,"escorter", **family.id**)
(The asterisks are just to show the change).
Then in the model I created a method that takes this argument:
def some_method(emails, type, **family_id**)
family = Family.find(family_id)
puts family.name
And it works.
So my question remains. Is there a problem to pass an object with rake?
There definitely shouldn't be a problem passing objects via method calls -- even if from a rake task.
If the family.name statement was actually what was failing I'd expect you'd see a message more like:
undefined method :name for #<Family>
So it sounds like you may be looking at the wrong line of code (i.e. it may be the fault of something else in the code). A missing attribute error sounds more like a mass-assignment problem.
Have you tried running the rake task with the --trace option?
rake the_task --trace
This may help identify the problem, either way.

How to retrieve <textarea> value in Ruby Cucumber unit testing tool

I am using the Cucumber unit test tool, and am trying to retrieve the value of a textarea, however I can't get it to work.
This is my code...
page.find(:xpath, "//div[#id='process']/table/tbody/tr/td/div/textarea").value
This is the error I'm getting...
Error: undefined method `value' for nil:NilClass (NoMethodError)
This URL listed below confirms that value is the correct method to retrieve the value:
http://www.w3schools.com/jsref/dom_obj_textarea.asp
Could someone please help me fix this problem.
The nil error indicates that you're not getting the textarea -- find is returning nil. Try a simpler xpath query, or referencing the textarea by name or id.

Resources