unit testing issue: wrong number of arguments (given 0, expected 1..2) - ruby-on-rails

I have an issue with my controller test, when running it in the terminal it return:
Error:
StoriesControllerTest#test_show_story:
ArgumentError: wrong number of arguments (given 0, expected 1..2)
test/controllers/stories_controller_test.rb:45:in `block in
<class:StoriesControllerTest>'
my test in stories_controller_test is:
test "show story" do
get story_path(stories(:one))
assert_response :success
assert_response.body.include?(stories(:one).name) #line 45
end
and in my stories.yml file I have:
one:
name: Bitcoin Reddit
link: https://www.reddit.com/r/Bitcoin/
If more is necessary please ask and the whole project is here.
Still beginning with unit testing and could not find a solution to this issue.

I guess what you're trying to use there is assert,
which would eventually fail if the argument isn't true. And as you're trying to check the body, then you must access response.body.
Edit your test adding the assert and pasing the include? on response.body asking whether it includes or not the stories(:one).name:
test "show story" do
get story_path(stories(:one))
assert_response :success
assert response.body.include?(stories(:one).name)
end

Related

Error while doing escape interpolation inside assert_select in rails 4.2

I am working on a project which is currently in rails-4.2.10 and
I want to do something like this:
assert_select ".note-nid-#{node.id}", false
But while testing it errors pops up:
ArgumentError: wrong number of arguments (given 3, expected 1)
while if I try:
assert_select '.note-nid-#{node.id}', false
no error pops up.
So basically how do I do escape interpolation inside assert_select in rails 4.2
I have found an answer to this problem. We can do it by
selector = css_select ".note-nid-#{node.id}"
assert_equal selector.size, 0

Error capybara js: true

Im having problems when i try to config my capybara test to respond js. This is my test:
test "creating_expense", js: true do
visit expenses_path
click_link('New Expense')
end
The error is:
/var/lib/gems/2.3.0/gems/activesupport-5.0.6/lib/active_support/testing/declarative.rb:11:in `test': wrong number of arguments (given 2, expected 1) (ArgumentError)
The default minitest test method doesn't support metadata on tests (the examples you're copying were probably using RSpec). To swap to the JS driver you need to change the current_driver in a setup blog as shown in the Capybara README - https://github.com/teamcapybara/capybara#using-capybara-with-minitest
If you want to add support for metadata on tests you can look at the minutest-minidata gem - https://github.com/wojtekmach/minitest-metadata#example-with-capybara

Rails App with Sinatra engine - How to avoid rails errors?

Problem: When running specs the output is very confusing, instead of saying where the error lies it just throws misleading errors
This is inside the rails lib folder, and it's mounted on the routes.rb
# lib/engines/users/app.rb
module Engines
module Users
class App < Sinatra::Base
get '/api/v1/users/me' do
raise 'runtime error here'
end
get '/api/v1/another-route' do
# something here
status 200
end
end
end
end
The spec file looks something like this:
it 'returns a 200' do
get '/api/v1/users/me', auth_attributes
expect(last_response.body).to eq 'something' # added to clarify my point, it's not the response status that I care of.
expect(last_response.status).to be 200
end
error:
Failure/Error: expect(last_response.status).to be 200
expected #<Fixnum:401> => 200
got #<Fixnum:1001> => 500
Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
`expect(actual).to eq(expected)` if you don't care about
object identity in this example.
expected error:
RuntimeError:
runtime error here
Another route also fails:
it 'something' do
get '/api/v1/another-route', auth_attributes
expect(last_response.status).to be 401
json = JSON.parse(last_response.body).with_indifferent_access
expect(json[:message]).to eql "You have been revoked access."
end
error: Prints a massive html output which I believe is the rails backtrace html output
expected error: none as this endpoint doesn't raise an error
My question is if there's a way to:
Stop rails from dealing with this, so it gives the actual output
Avoid the entire engine to fail because one route raise exception
I believe that by solving the first point, the second one gets fixed too.
Thank you for your time
In order to solve my problem I've discovered that on the spec_helper the ENV['RACK_ENV'] was not being set, setting it to test resolves the problem and throws the exception I need in order to debug my code.
This happens because https://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb#L1832
set :show_exceptions, Proc.new { development? }
development? returned true when in fact should be false (needed the RACK_ENV set to test)
Now I get the correct output.

not_to change.by() is not supported

I upgraded rspec version from 2 to 3. This is one of the issues I faced:
Failures:
1) Slide after .destroy(force: false) visible if .with_deleted
Failure/Error: expect{#slide.destroy(force: false)}.to_not change(Slide.with_deleted, :count).by(1)
NotImplementedError:
`expect { }.not_to change { }.by()` is not supported
# ./spec/models/slide_spec.rb:36:in `block (3 levels) in <top (required)>'
and in the rspec's changelog I can read it was never supported (oink ?!##). At the same time there are still some examples how to use change syntax but without not keyword.
So the question is how to expect no change ?
Fortunately I want to expect no change (any) so I can omit by() part. It works just fine !
expect{#slide.destroy(force: false)}.to_not change(Slide.with_deleted, :count)

cucumber contradictory error messages

Problem Background: "cucumber declarative step definitions using web_steps.rb" Stack Overflow Question
In troubleshooting the problem in question two contradictory error messages are arrived at; with the statement:
When /^(?:|I )uncheck "([^"]*)"$/ do |field|
check(field)
end
added to 'features/step_definitions/movie_steps.rb' execution of 'bundle exec cucumber features/filter_movie_list.feature' results in:
Ambiguous match of "I uncheck "ratings_G"":
features/step_definitions/movie_steps.rb:65:in '/^(?:|I )uncheck "([^"]*)"$/
features/step_definitions/web_steps.rb:65:in '/^(?:|I )uncheck "([^"]*)"$/
However, removal of the step does not result in the step definition from 'web_steps.rb' being utilized; rather, a different error message is displayed:
When I uncheck the following ratings: G, PG-13 # features/step_definitions/movie_steps.rb:44
Undefined step: "When I uncheck "ratings_G"" (Cucumber::Undefined)
./features/step_definitions/movie_steps.rb:52:in `block (2 levels) in <top (required)>'
./features/step_definitions/movie_steps.rb:49:in `each'
./features/step_definitions/movie_steps.rb:49:in `/I (un)?check the following ratings: (.*)/'
features/filter_movie_list.feature:30:in `When I uncheck the following ratings: G, PG-13'
How is it possible for Cucumber to complain that a step is redundant when there are two definitions in two places but then later complain that the very same step is not defined when its duplicate is removed? Is it possible the second error message really means something other than what is stated?
PS: The configuration was arrived at by way of a Cucumber installation with training wheels for CS169.x1 # edX...
It seems that you have one definition called When I uncheck "ratings_G" that is somewhat matched with the call When I uncheck the following ratings: G, P-13 that occurs in the error message. Cucumber is complaining that nothing matches the latter one. Does it exist in your code?
The somewhat matching would possibly explain the ambiguity warning you got initially. The definition matches but not what is found inside the string.

Resources