I'm in the middle of the upgrade of my application. I want to move from ruby 1.8 to 1.9 and from rails 3.0 to 3.2 (right now I run ruby 1.9 and RoR 3.2). I have problems with my tests: the output is vague and not very helpful. For example for assert_difference I get:
Failure:
test_should_create_new_version_if_versioning_needed_and_diaconies_changed(AnimatorProfilesControllerTest)
test/functional/animator_profiles_controller_test.rb:108:in `block in <class:SomeTest>'
=> 108: assert_difference('SomeModel.count', 1) do
109: put :update, :id => #model.to_param, :model => attrs
110: end
111:
<> expected but was
<>
diff:
nil
The problems is that there is no diff (as you can see). The other problem is that when I have a message in assert, it is not shown when it fails. How could I fix it?
Related
I am using Fabrication gem v-2.5.0 on Rails 2.3.16 but get the following errors when I run the unit test cases:
Below is the code snippet :
First case
Fabricate(:some_modal)
Fabrication::MisplacedFabricateError: # from /Users/user_xyz/.rvm/gems/ree-1.8.7- 2011.03#project/gems/fabrication-2.5.0/lib/fabrication.rb:51:in `Fabricate' from (irb):3
Second case
Fabricate(:some_other_modal)
SyntaxError: /Users/user_xyz/.rvm/gems/ree-1.8.7-2011.03#project/gems/fabrication-2.5.0/lib/fabrication/generator/active_record.rb:8: syntax error, unexpected ':', expecting ')' ...ttributes, without_protection: true)
Can someone please help me resolve these.
Modal Class :
class ErrorCode
attr_accessor :mappings
has_many :error_code_mappings
end
Fabricator :
Fabricator(:error_code) do
application_id 77
error_code_mappings(:count => 3) { |error_code, i| Fabricate.build(:error_code_mapping, :error_code => Fabricate.build(:error_code, :code => error_code.code + i))}
end
Unit test file:
require 'test_helper'
class ErrorCodeTest < ActiveSupport::TestCase
context "ErrorCode" do
setup do
#error_code = Fabricate.build(:error_code)
assert(#error_code.valid?)
end
should "have setter for mapping attribute" do
assert_respond_to(#error_code, :mappings=)
end
end
Fabrication requires Ruby 1.9 and higer version. And the current version of ruby that is being used as per the given code snippets is REE 1.8.7.
Upgrade your ruby version & then u can get it working!
I am getting syntactic errors in ruby files in my system although these are not there in other systems.
The errors are in code like:
1) redirect_to :back, alert: exception.message
The syntactic error is in alert
2) load_and_authorize_resource only: [ :update, :destroy ]
The syntactic error is in only
3) render json: #reward.to_json
The syntactic error is in json
Like that there are number of errors.
I am getting rid of these errors by doing following changes to the above:
1) redirect_to :back, :alert => exception.message
2) load_and_authorize_resource :only => [ :update, :destroy ]
3) render :json => #reward.to_json
It seems that error is due to ruby version but I am not sure what's the proper reason is?
I need to do all such changes in every existing projects and is quite painful.
Does that imply that the projects are using old ruby syntax or does that imply that I have old ruby version installed?
My ruby version is ruby 1.9.2p180
Also if its ruby version problem than does the ruby upgrade to the latest version will affect the whole project and how to accomplish this easily ?
UPDATE:
I confirmed that my ruby version is ruby 1.9.2p180 because of the following:
D:\ruby_work>ruby -v
ruby 1.9.2p180 (2011-02-18) [i386-mingw32]
D:\ruby_work>pik list
187: ruby 1.8.7 (2010-06-23 patchlevel 299) [i386-mingw32]
* 192: ruby 1.9.2p180 (2011-02-18) [i386-mingw32]
The asteric(*) above is indicating the version I am currently using.
The exact errors are for example in redirect_to :back, :alert => exception.message is-
, unexpected ':'
UPDATE:
Now I did -
D:\ruby_work>pik use 187
D:\ruby_work>pik list
* 187: ruby 1.8.7 (2010-06-23 patchlevel 299) [i386-mingw32]
192: ruby 1.9.2p180 (2011-02-18) [i386-mingw32]
D:\ruby_work>ruby -v
ruby 1.8.7 (2010-06-23 patchlevel 299) [i386-mingw32]
The error is still there. Now I suspect Is it really a ruby error because changing to older ruby version also showing the same syntactic error. The only difference is that now even changing from :key to key => is also showing the same error.
Older versions of Ruby didn't understand the hash notation of key: value, only :key => value, which 1.9+ understands.
I don't remember when the new notation was added, but, as you found, the fix to allow the code to run on older versions is to use the original notation.
In Ruby 1.8.7, this is the error I get using IRB:
irb(main):001:0> foo = {a:'b'}
SyntaxError: compile error
(irb):1: syntax error, unexpected tSYMBEG, expecting kDO or '{' or '('
foo = {a:'b'}
^
(irb):1: syntax error, unexpected '}', expecting $end
from (irb):1
irb(main):002:0> foo = {:a => 'b'}
=> {:a=>"b"}
I am seeing this error in a delayed job on heroku and it makes no sense to me:
{uninitialized constant Less::Engine
(in /app/app/assets/stylesheets/share_and_earn_recommendation_email.css.less)
/app/vendor/bundle/ruby/1.9.1/gems/tilt-1.3.3/lib/tilt/css.rb:60:in `prepare'
...
Why no sense? Because css.rb looks like this:
def prepare
if ::Less.const_defined? :Engine
#engine = ::Less::Engine.new(data) # line 60
else
...
Which means it is impossible to hit line 60 if Less::Engine is undefined. What am I missing?
EDIT
Even better demonstration from heroku console:
irb(main):008:0> ::Less.const_defined? :Engine
=> true
irb(main):009:0> ::Less::Engine
NameError: uninitialized constant Less::Engine
EDIT 2
It gets more interesting:
irb(main):011:0> ::Less.const_defined? :Engine, false
=> false
The difference is that the latter does not search ancestors. But there are no ancestors, so it should not make a difference:
irb(main):012:0> ::Less.ancestors
=> [Less]
If you just recently upgraded your rails version within the 3.2.x stack you will find that less is "present" in earlier versions like 3.2.2 and absent in later versions like 3.2.9.
I haven't fully investigated the issue, but I noticed when I went to upgrade from 3.2.2 to 3.2.9, I got some "less" issues.
Cheers
Using rvm with ree-1.8.7-2011.03 and rspec-1.3.2, getting an error trying to run a spec file with Rails 2.3.14
Any help is appreciated since I can't run any spec tests. Thanks!
Running the spec file from my Rails folder like:
spec spec/models/assignment_spec.rb
~/.rvm/gems/ree-1.8.7-2011.03/gems/rspec-1.3.2/lib/spec/runner/options.rb:247:in `initialize': wrong number of arguments (1 for 2) (ArgumentError)
from ~/.rvm/gems/ree-1.8.7-2011.03/gems/rspec-1.3.2/lib/spec/runner/options.rb:247:in `new'
from ~/.rvm/gems/ree-1.8.7-2011.03/gems/rspec-1.3.2/lib/spec/runner/options.rb:247:in `load_formatters'
...
~/.rvm/gems/ree-1.8.7-2011.03/gems/rspec-1.3.2/lib/spec/runner/.rb:
...
def load_formatters(format_options, formatters)
format_options.map do |format, where|
formatter_type = if formatters[format]
require formatters[format][0]
eval(formatters[format][1], binding, __FILE__, __LINE__)
else
load_class(format, 'formatter', '--format')
end
formatter_type.new(formatter_options) # <-- 247
end
end
def formatter_options
#formatter_options ||= OpenStruct.new(
:colour => colour,
:autospec => autospec,
:dry_run => dry_run
)
end
...
EDIT:
spec_helper.rb:
https://github.com/instructure/canvas-lms/blob/stable/spec/spec_helper.rb
./.spec.opts:
--colour
--require spec/nested_instafail_formatter
--format RSpec::NestedInstafailFormatter
--format html:tmp/spec_html/index.html
"have you tried with ree-1.8.7-2012.02 ?" – mpapis yesterday
Mpapis answered this question, the spec test ran using ree-1.8.7-2012.02 with rspec-1.3.2,.
def mock_category(stubs={})
#mock_category ||= mock_model(Category, stubs).as_null_object
end
describe "GET show" do
it "assigns the requested category as #category" do
Category.stub(:find).with("37") { mock_category }
get :show, :id => "37"
assigns(:category).should be(mock_category)
end
end
Which returns :
1) CategoriesController GET show assigns the requested category as #category
Failure/Error: assigns(:category).should be(mock_category)
expected Category_1002, got nil
I'm confused here, because this is a right out of the box controller that rspec set up. Why could this be failing?
My versions:
Rails 3.0.0.beta4
Ruby 1.8.7
RSpec 2.0.0.beta.10
Also tried this, same exact reproducible error with :
Rails 3.0.0
Ruby 1.8.7
RSpec 2.0.0.beta.20
The command I used to generate the specs were rails g scaffold Category
In my application.rb
config.generators do |g|
g.template_engine :haml
g.test_framework :rspec, :fixture => true, :views => false
end
UPDATE
This goes for any scaffolded controller by Rails 3, with RSpec2. Its guarenteed to fail. Anyone know how this is supposed to be written?
rspec-rails has a spec-suite it runs against itself that uses all the generators and runs all the generated specs and they all pass, so this should work. What versions of rspec, rails, and ruby are you using? What commands did you use to generate the Category model and CategoriesController?
The conflict comes from conflicts that occurred between Rspec Beta 10 and Rspec Beta 20, and Rails 3 Beta4, to Rails 3 release.
To solve this, I uninstalled haml, and installed haml-rails.
Then I deleted all the specs that were previously generated, and regenerated them.