Test fails when run together but passes alone - ruby-on-rails-3

I am new to RSPEC and am testing the controllers.
Sometimes it happens that the test passes alone like
rspec spec/controllers/controller_name_spec.rb
it passes but when I run :-
rspec spec
the test fails, same test that passed earlier.
What can be the reason, is it related to scalability of rspec or an application specific issue.
As I found same issue with integration tests also.
Rails -v => 3.2.11
Ruby -v => 1.9.2p320
Rspec-rails version => 2.13.0
rspec version => 2.13.0
eg:-
Test case :-
it "should do ##something##" do
plans=Plan.pluck(:id)
plans.delete(#user.subscription.plan.id)
#user.subscription.stripe_customer_token= "cus_1l9m6CicQEXZJ0"
#user.save
get 'apply_change_account', {:subscription=>{:plan_id=>plans.sample}}
flash.now[:success].should_not be_nil
response.should redirect_to dashboard_manage_accounts_path
end
Response error message :-
1) DashboardController Dashboard should do ##something##
Failure/Error: flash.now[:success].should_not be_nil
expected: not nil
got: nil
# ./spec/controllers/dashboard_controller_spec.rb:119:in `block (2 levels) in <top (required)>'
Thanks in advance

Try add flash.clear in before block for every test related to flash.
This may or may not solve the problem, but it's essential to test flash related item to avoid flash object been polluted.

Related

Replace 'alias_method_chain :save_attachments, :pasted_images' in Rails 5.2.3

I've got a Redmine 3.4.4 installation running, which uses Ruby 2.2.5-p319 and Rails 4.2.8. We want to upgrade this to Redmine latest (currently 4.0.4) which requires Rails 5.
I'm running the new server using 4.0.4 using Ruby 2.6.3-p62 and Rails 5.2.3. Overall it's OK, but we have a number of plugins installed we would like to migrate. Many of these have had problems because of deprecations in Rails 5. I've managed to muddle my way through 8 of 9 plugins, even though I've not written Ruby on Rails before, but I'm stuck on the last one and just can't figure it out.
The plugin is the My Page Customization plugin and when I attempt to migrate the database and plugins I get this error:
[centos#redmine]$ bundle exec rake db:migrate RAILS_ENV=production
rake aborted!
NoMethodError: undefined method `alias_method_chain' for ActivitiesController:Class
Did you mean? alias_method
/usr/local/src/redmine-4.0.4/plugins/redmine_my_page/lib/my_page_patches/activities_controller_patch.rb:11:in `block in included'
/usr/local/src/redmine-4.0.4/plugins/redmine_my_page/lib/my_page_patches/activities_controller_patch.rb:7:in `class_eval'
/usr/local/src/redmine-4.0.4/plugins/redmine_my_page/lib/my_page_patches/activities_controller_patch.rb:7:in `included'
/usr/local/src/redmine-4.0.4/plugins/redmine_my_page/init.rb:30:in `include'
/usr/local/src/redmine-4.0.4/plugins/redmine_my_page/init.rb:30:in `block (2 levels) in <top (required)>'
So, it's clear that the deprecated 'alias_method_chain' is the issue here. After some digging I found a lot of references online such as this one which is good and clear, but I just can't get code written that works - I keep getting syntax errors and can't figure out what I'm doing wrong.
This is the original snippet from activities_controller_patch.rb:
module ActivitiesControllerPatch
def self.included(base) # :nodoc:
base.send(:include, InstanceMethods)
base.class_eval do
unloadable
helper :issues
helper :queries
alias_method_chain :index, :esi
end
end
We'd like to hang onto this plugin if we can even though it doesn't officially support Redmine 4. I'm hoping someone with better Ruby knowledge will be able to help.
Instead of
alias_method_chain :index, :esi
you'll just use
alias_method :index_without_esi, :index
alias_method :index, :index_with_esi
It was some kind of syntactic sugar.

Chapter 5 - Ruby on Rails Tutorial - undefined method `has_title?' when running RSpec tests

Just trying to get through the last bit of chapter 5 in Michael Hartl's Ruby on Rails Tutorial and getting another error running RSpec tests.
The output is :
Static pages should have the right links on the layout
Failure/Error: expect(page).to have_title('About Us')
NoMethodError:
undefined method has_title?' for #<Capybara::Session>
# ./spec/requests/static_pages_spec.rb:59:inblock (2 levels) in '
and results from the line starting expect(page) in the following code in static_pages_spec.rb :
it "should have the right links on the layout" do
visit root_path
click_link "About"
expect(page).to have_title(full_title('About Us'))
end
Note : This happens running with or without Spork
Can anyone point me in the right direction please ?
Thanks,
Bazza
The have_title function is supported from Capybara 2.1. I suppose you have an older version of Capybara in your Gemfile. So, update your Gemfile with
gem 'capybara', '2.1.0'
then update Capybara like this
bundle update capybara
and rerun the specs using rspec. It should work now
Refer to this post for other options

Rails Tutorial - weird RSpec behavior

I'm currently in section 10.3.2 of the Rails Tutorial (beta) and ran into some problems with RSpec.
rspec spec/ runs perfectly, however if I do rspec spec/requests/micropost_pages_spec.rb as described in the tutorial I get the following error:
Failures:
1) Micropost pages micropost creation with invalid information should not create a micropost
Failure/Error: before { visit root_path }
ActionView::Template::Error:
undefined method `model_name' for NilClass:Class
# ./app/views/shared/_micropost_form.html.erb:1:in `_app_views_shared__micropost_form_html_erb__567210303736562711_70234980477780'
# ./app/views/static_pages/home.html.erb:8:in `_app_views_static_pages_home_html_erb___61006871036382679_70234970424160'
# ./spec/requests/micropost_pages_spec.rb:11:in `block (3 levels) in <top (required)>'
Performing the test manually in the web browser seems to work perfectly as well.
Not sure if I did something wrong or if there's a "bug" in the tutorial. Can anyone give a hint?
UPDATE 1: Here's a link to the whole project on GitHub.
UPDATE 2: I now found out that when I restart the rails server I get the error, too, when I hit the page the first time. But when I load the page a second time (reload) then it works perfectly.
Found the problem. Rather stupid mistake. In my static_pages_controller.rb I had
def home
def home
#micropost = current_user.microposts.build if signed_in?
end
end
Thanks anyway for the tips.

Guard w/ RSpec2, Ruby1.9.3, Rails 3.1.3

I am running Guard successfully with notifications provided by Libnotify.
funkdified#funkdified-laptop:~/railsprojects/sample_app$ guard
Guard uses Libnotify to send notifications.
Guard is now watching at '/home/funkdified/railsprojects/sample_app'
Guard::RSpec is running, with RSpec 2!
Running all specs
...
Finished in 0.06053 seconds
3 examples, 0 failures
If I modify the spec file I am alerted as to the results of the test, both in the terminal and with a popup notification. If I modify a controller file, again tests run normally. However, when I modify routes.rb, the whole things falls on its face and Guard stops working normally and throws up an error. Anyone have any ideas?
Error:
Running: spec/routing
/home/funkdified/.rvm/gems/ruby-1.9.3-p0#rails3tutorial/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `load': cannot load such file -- /home/funkdified/railsprojects/sample_app/spec/routing (LoadError)
from /home/funkdified/.rvm/gems/ruby-1.9.3-p0#rails3tutorial/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `block in load_spec_files'
from /home/funkdified/.rvm/gems/ruby-1.9.3-p0#rails3tutorial/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `map'
from /home/funkdified/.rvm/gems/ruby-1.9.3-p0#rails3tutorial/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `load_spec_files'
from /home/funkdified/.rvm/gems/ruby-1.9.3-p0#rails3tutorial/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:22:in `run'
from /home/funkdified/.rvm/gems/ruby-1.9.3-p0#rails3tutorial/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:80:in `run_in_process'
from /home/funkdified/.rvm/gems/ruby-1.9.3-p0#rails3tutorial/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:69:in `run'
from /home/funkdified/.rvm/gems/ruby-1.9.3-p0#rails3tutorial/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:10:in `block in autorun
Forgot to mention, if I kill Guard and restart (after making changes to routes.rb) then Guard again performs normally, suggesting that a test has now failed:
funkdified#funkdified-laptop:~/railsprojects/sample_app$ guard
Guard uses Libnotify to send notifications.
Guard is now watching at '/home/funkdified/railsprojects/sample_app'
Guard::RSpec is running, with RSpec 2!
Running all specs
..F
Failures:
1) PagesController GET 'about' returns http success
Failure/Error: get 'about'
ActionController::RoutingError:
No route matches {:controller=>"pages", :action=>"about"}
# ./spec/controllers/pages_controller_spec.rb:22:in `block (3 levels) in <top (required)>'
Finished in 0.0576 seconds
3 examples, 1 failure
Failed examples:
rspec ./spec/controllers/pages_controller_spec.rb:21 # PagesController GET 'about' returns http success
Check your spec directory. It should have a routing sub-directory for routing specs. If not, just create an empty one. Apparently, RSpec does not create any sub-directories under spec when running rails g rspec:install, but guard expects it to be there.
I think you may have the following line in your Guardfile:
watch('config/routes.rb') { "spec/routing" }
If you don't have specs in spec/routing, then don't folder that you don't need. Change the line to:
watch('config/routes.rb') { "spec" }
Now, all your specs will run when you update your routes.rb file.

ruby on rails tutorial - rails 3.0 chapter 11 rspec fails after code listing 11.27

I have run through the book. I run "rspec spec/" after each step.
Chapter 11 fails starting listin 11.27 adding app/views/pages/home.html.erb
After Listing 11.30. Adding a micropost instance variable to the home action.
app/controllers/pages_controller.rb
Everything is supposed to be working. It isn't. I am just digging into rails for the very first time. since this was cut and paste code, I did not expect errors.
I will be grateful for any help in leaning to debug this. Firure out if I cut and paste wrong, or if Michel's code example is not fully tested.
Here is the rspec output:
MPECHNER-MBP:sample_app michael.pechner$ rspec spec/
..FF.............................................................................................FF....
Failures:
1) MicropostsController POST 'create' failure should not create a micropost
Failure/Error: post :create, :micropost => #attr
ActionView::Template::Error:
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.errors
# ./app/views/shared/_error_messages.html.erb:1:in _app_views_shared__error_messages_html_erb___2864847557408089106_2179824440__3563936241835894680'
# ./app/views/shared/_micropost_form.html.erb:2:inblock in _app_views_shared__micropost_form_html_erb_732794408121469484_2179844600__1418926388395345268'
# ./app/views/shared/_micropost_form.html.erb:1:in _app_views_shared__micropost_form_html_erb___732794408121469484_2179844600__1418926388395345268'
# ./app/views/pages/home.html.erb:6:in_app_views_pages_home_html_erb_1781066003698849377_2179862020__1594538359887050056'
# ./app/controllers/microposts_controller.rb:10:in create'
# ./spec/controllers/microposts_controller_spec.rb:34:inblock (5 levels) in '
# ./spec/controllers/microposts_controller_spec.rb:33:in `block (4 levels) in '
2) MicropostsController POST 'create' failure should render the home page
Failure/Error: post :create, :micropost => #attr
ActionView::Template::Error:
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.errors
# ./app/views/shared/_error_messages.html.erb:1:in _app_views_shared__error_messages_html_erb___2864847557408089106_2179824440__3563936241835894680'
# ./app/views/shared/_micropost_form.html.erb:2:inblock in _app_views_shared__micropost_form_html_erb_732794408121469484_2179844600__1418926388395345268'
# ./app/views/shared/_micropost_form.html.erb:1:in _app_views_shared__micropost_form_html_erb___732794408121469484_2179844600__1418926388395345268'
# ./app/views/pages/home.html.erb:6:in_app_views_pages_home_html_erb_1781066003698849377_2179862020__1594538359887050056'
# ./app/controllers/microposts_controller.rb:10:in create'
# ./spec/controllers/microposts_controller_spec.rb:39:inblock (4 levels) in '
3) LayoutLinks when signed in should have a signout link
Failure/Error: visit root_path
ActionView::Template::Error:
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.errors
# ./app/views/shared/_error_messages.html.erb:1:in _app_views_shared__error_messages_html_erb___2864847557408089106_2179824440__3563936241835894680'
# ./app/views/shared/_micropost_form.html.erb:2:inblock in _app_views_shared__micropost_form_html_erb_732794408121469484_2179844600__1418926388395345268'
# ./app/views/shared/_micropost_form.html.erb:1:in _app_views_shared__micropost_form_html_erb___732794408121469484_2179844600__1418926388395345268'
# ./app/views/pages/home.html.erb:6:in_app_views_pages_home_html_erb_1781066003698849377_2179862020__1594538359887050056'
# :10:in synchronize'
# ./spec/requests/layout_links_spec.rb:51:inblock (3 levels) in '
4) LayoutLinks when signed in should have a profile link
Failure/Error: visit root_path
ActionView::Template::Error:
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.errors
# ./app/views/shared/_error_messages.html.erb:1:in _app_views_shared__error_messages_html_erb___2864847557408089106_2179824440__3563936241835894680'
# ./app/views/shared/_micropost_form.html.erb:2:inblock in _app_views_shared__micropost_form_html_erb_732794408121469484_2179844600__1418926388395345268'
# ./app/views/shared/_micropost_form.html.erb:1:in _app_views_shared__micropost_form_html_erb___732794408121469484_2179844600__1418926388395345268'
# ./app/views/pages/home.html.erb:6:in_app_views_pages_home_html_erb_1781066003698849377_2179862020__1594538359887050056'
# :10:in synchronize'
# ./spec/requests/layout_links_spec.rb:57:inblock (3 levels) in '
Finished in 2.04 seconds
103 examples, 4 failures
MPECHNER-MBP:sample_app michael.pechner$
I searched git hub for others that have worked through this.
I found it in the posting by https://github.com/mfmcgran
"app/views/shared/_error_messages.html.erb" seems to be wrong, The code I had showed the first line as "if #users.errors.any?" ad it needs to be "if object.errors.any?"
There was no mention in chapter 11 that this needed to changed. But after this change, all 103 rpsec tests passed.
The tutorial has gotten a little older (given the speed at which gem versions get released). I find its really convenient if you just upgrade to the most recent version of rspec and use capybara instead
gem 'capybara'
in your Gemfile and
gem 'capybara/rspec'
in your spec_helper.rb and run
bundle install
this should then work
click_link 'About'
page.should have_content("About")
# in place of
# enter code here`response.should have_selector('title', :content => "Contact")
this will continue to work as well
get '/about'
response.should have_selector('title', :content => "About")