Fabrication::MisplacedFabricateError while using Fabrication Gem on Rails 2.3.16 - ruby-on-rails

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!

Related

shoulda-matcher `belongs_to` is producing this error: undefined method `primary_key_name'

I have a Rails 4 application with a belongs_to relationship which I am trying to test with shoulda-matcher:
The model:
class Foo < ActiveRecord::Base
belongs_to :bar
end
The test:
require 'rails_helper'
Rspec.describe Foo, type: :model do
it { should belong_to :bar }
end
Produces the following error:
NoMethodError:
undefined method `primary_key_name' for #<ActiveRecord::Reflection::BelongsToReflection:0x007fde9b87e368>
Did you mean? primary_key_type
If I use the uglier:
it { Foo.reflect_on_association(:bar).macro.should eq(:belongs_to) }
The test passes.
I have seen this SO and this GitHub issue, but they refer to a different matcher and blames the validates_existence gem which I do not have installed.
I can live with the long-hand syntax, but any advice would be greatly appreciated. Thanks in advance!
EDIT:
It looks like my problem was related to gem versions. I changed the versions of shoulda and shoulda-matchers and the relationship matchers now work correctly.
Original gem versions:
rails: 4.2.6
shoulda: 2.11.3
shoulda-matchers: 3.1.1
Working gem versions:
shoulda: 3.5.0
shoulda-matchers: 2.8.0

What changed in Ruby?

The following spec passes fine in Ruby 2.1.5 but fails in 2.2.0 and I can't tell what that's all about:
# job.rb
class Job < ActiveRecord::Base
validates :link, :url => true
end
# job_spec.rb
require 'rails_helper'
describe Job do
describe "#create" do
["blah", "http://", " "].each do |bad_link|
it {
should_not allow_value(bad_link).for(:link)
}
end
end
end
fail log looks like this:
1) Job#create should not allow link to be set to "http://"
Failure/Error: should_not allow_value(bad_link).for(:link)
Expected errors when link is set to "http://",
got no errors
# ./spec/models/job_spec.rb:14:in `block (4 levels) in <top (required)>'
I find the only way to for that spec to pass with Ruby 2.2.0 is to include the validates_url gem in my project!!
Does anyone know this is about?
Maybe my solution isn't ideal, but it works.
Replace validates_url gem by validates gem. It has UrlValidator (written by me), which is well tested.
gem 'validates' # in Gemfile
validates :link, :url => true # you needn't to change something. Just remove validates_url from your Gemfile
P.S. It's a strange way - to test functionality of gem. Functionality should be tested in gem already.
P.P.S. I'm strongly recommend you to move to ruby 2.2.1 (or 2.2.2) instead of 2.2.0, because of 2.2.0 has a lot of bugs

pry on rspec in cloud 9 ide

I am new to RoR. I have two problems here.
ISSUE 1
my rspec is
require 'spec_helper'
describe RefUserCategory do
describe "validations" do
it { should validate_presence_of(:user_category_description) }
end
end
my Ref_User_Category model is
class RefUserCategory < ActiveRecord::Base
validate :user_category_description , presence: true
has_many :Users
end
The error I got in Rspec is
Expected errors to include /can't be blank/ when user_category_description is set to nil, got no errors
So I decided to to use Pry to check whats happening
ISSUE 2
Im my application folder I do
pry -r ./config/environment
[2] pry(main)> Ref_User_Category.new
LoadError: Unable to autoload constant Ref_User_Category, expected /var/lib/stickshift/52d29d0e5004461024000031/app-root/data/736003/east/app/models/ref_user_category.rb to define it
from /var/lib/stickshift/52d29d0e5004461024000031/app-root/data/lib/ruby/gems/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:464:in `load_missing_constant'
I have user model too I
pry(main)> User.new
gives error
ActiveRecord::StatementInvalid: Could not find table 'users'
/app-root/data/lib/ruby/gems/gems/activerecord-4.0.2/lib/active_record/connection_adapters/sqlite3_adapter.rb:512:in `table_structure'
Regarding issue 1, you have a typo: change validate to validates.
Regarding issue 2:
You generally start the console from the root of your project like this:
$ rails c
You are doing Ref_User_Category.new when you should do RefUserCategory.new.
About User.new, it looks like you haven't run your database migrations:
$ rake db:migrate
$ rake db:test:prepare

Ruby: How to make a public static method?

In Java I might do:
public static void doSomething();
And then I can access the method statically without making an instance:
className.doSomething();
How can I do that in Ruby? this is my class and from my understanding self. makes the method static:
class Ask
def self.make_permalink(phrase)
phrase.strip.downcase.gsub! /\ +/, '-'
end
end
But when i try to call:
Ask.make_permalink("make a slug out of this line")
I get:
undefined method `make_permalink' for Ask:Class
Why is that if i haven't declared the method to be private?
Your given example is working very well
class Ask
def self.make_permalink(phrase)
phrase.strip.downcase.gsub! /\ +/, '-'
end
end
Ask.make_permalink("make a slug out of this line")
I tried in 1.8.7 and also in 1.9.3
Do you have a typo in you original script?
All the best
There is one more syntax which is has the benefit that you can add more static methods
class TestClass
# all methods in this block are static
class << self
def first_method
# body omitted
end
def second_method_etc
# body omitted
end
end
# more typing because of the self. but much clear that the method is static
def self.first_method
# body omitted
end
def self.second_method_etc
# body omitted
end
end
Here's my copy/paste of your code into IRB. Seems to work fine.
$ irb
1.8.7 :001 > class Ask
1.8.7 :002?>
1.8.7 :003 > def self.make_permalink(phrase)
1.8.7 :004?> phrase.strip.downcase.gsub! /\ +/, '-'
1.8.7 :005?> end
1.8.7 :006?>
1.8.7 :007 > end
=> nil
1.8.7 :008 > Ask.make_permalink("make a slug out of this line")
=> "make-a-slug-out-of-this-line"
Seems to work. Test it out in your irb as well, and see what results you're getting. I'm using 1.8.7 in this example, but I also tried it in a Ruby 1.9.3 session and it worked identically.
Are you using MRI as your Ruby implementation (not that I think that should make a difference in this case)?
In irb make a call to Ask.public_methods and make sure your method name is in the list. For example:
1.8.7 :008 > Ask.public_methods
=> [:make_permalink, :allocate, :new, :superclass, :freeze, :===,
...etc, etc.]
Since you also marked this as a ruby-on-rails question, if you want to troubleshoot the actual model in your app, you can of course use the rails console: (bundle exec rails c) and verify the publicness of the method in question.
I am using ruby 1.9.3 and the program is running smoothly in my irb as well.
1.9.3-p286 :001 > class Ask
1.9.3-p286 :002?> def self.make_permalink(phrase)
1.9.3-p286 :003?> phrase.strip.downcase.gsub! /\ +/, '-'
1.9.3-p286 :004?> end
1.9.3-p286 :005?> end
=> nil
1.9.3-p286 :006 > Ask.make_permalink("make a slug out of this line")
=> "make-a-slug-out-of-this-line"
It's also working in my test script. Nothing wrong with your given code.It's fine.

Anyone know why I'm getting this RSpec error? getting nil <--

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.

Resources