not_to change.by() is not supported - ruby-on-rails

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)

Related

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

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

RSpec deprecated behavior 'let'

I am receiving these warnings when running my RSpec tests using Rspec-rails 2.14. I want to upgrade to RSpec 3 to see if there are any performance improvements, but I assume these will all fail due to this warning? I am using Factory girl also.
This is deprecated behavior that will not be supported in RSpec 3.
`let` and `subject` declarations are not intended to be called
in a `before(:all)` hook, as they exist to define state that
is reset between each example, while `before(:all)` exists to
define state that is shared across examples in an example group.
WARNING: let declaration `fa` accessed in a `before(:all)` hook at:
D:/RailsApps/uwb/spec/models/fa_commercial_spec.rb:77:in `block (2 level
s) in <top (required)>'
Here is one example, how can this be changed so that it will be ok in RSpec 3?
require 'spec_helper'
describe 'FaCommercial' do
let(:admin_user) {create(:user_admin_dev)}
let(:template) {create :fa_commercial_template}
let(:fa) {create :fa_commercial, fa_template: template}
let(:stmt_template_bal_sheet) {create :fa_commercial_stmt_template_bal_sheet, fa_template: template}
let(:stmt_template_cash_flow) {create :fa_commercial_stmt_template_cash_flow, fa_template: template}
let(:stmt_template_income_stmt) {create :fa_commercial_stmt_template_income_stmt, fa_template: template}
let(:stmt_template_ratios) {create :fa_commercial_stmt_template_ratios, fa_template: template}
before(:all) do
admin_user
stmt_template_bal_sheet
stmt_template_cash_flow
stmt_template_income_stmt
stmt_template_ratios
..........
The error is self-explanatory.
`let` and `subject` declarations are not intended to be called
in a `before(:all)` hook
You might take a look at the RSpec documentation to understand how things works first, then, you will be able to refactor your scenarios in order to make it work.

When I run the test "rspec spec "In the console, I'll get a deprecation warning

describe Item do
it 'calculates price according to a special formula' do
item = Item.new('kettle', price: 200)
item.price.should == 212
end
end
Deprecation Warnings:
Using should from rspec-expectations' old :should syntax without explicitly enabling the syntax is deprecated. Use the new :expect syntax or explicitly enable :should with
config.expect_with(:rspec) { |c| c.syntax = :should } instead. Called from E:/work/storeapp/spec/item_spec.rb:9:in `block (2 levels) in '.
If you need more of the backtrace for any of these deprecations to
identify where to make the necessary changes, you can configure
config.raise_errors_for_deprecations!, and it will turn the
deprecation warnings into errors, giving you the full backtrace.
1 deprecation warning total
Finished in 0.00505 seconds (files took 0.17058 seconds to load)
1 example, 0 failures
How this warning can be avoided?
Write the test in a new style:
expect(item.price).to eq 212
BTW. it seems you might doing sth quite risk/confusing. Once you assign 200 to the attribute, it will be more than confusing to see another value returned by a getter with a same name. Have you considered leaving the original method alone and defining a new one instead (like price_with_vat)?

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.

How to display rspec errors faster?

My rspec test suite is slow. It takes about half a decade to run all the tests. While it's running, I see only that tests are failing.
.......................................................................
.......FFFFFFFFFFFFFFF....F..........FFFFFFFFF.........................
................FFFFFFFFFFFFFFF.....................FF.................
..........................FFF..........................................
.............FFFFFFFFFFFFFFFFFFFFFF....................................
................................................................FFFF...
.......FFFFFFFFF..........................
Then, after staring at this for a few years, I finally get a listing of what is wrong.
Failures:
172481) Foobar should barfoo the barbaz while quux is set to narf
Failure/Error: before { click_link "Enable narf" }
ArgumentError:
wrong number of arguments (0 for 1)
# ./app/helpers/foobar_helper.rb:22:in `gobble'
# ./app/controllers/barbaz_controller.rb:18:in `omgwtf'
# (eval):2:in `click_link'
# ./spec/requests/metasyntactic_spec.rb:43:in `block (5 levels) in <top (required)>'
Is there a way to tell rspec it should display the errors directly - while running the test suite?
You can use the fuubar format, see the doc here : https://github.com/jeffkreeftmeijer/fuubar/

Resources