I want to extend the faker gem for rails to also generate other random date (in my case computer game names)
#lib/extended_faker.rb
require 'faker'
require 'extended_faker/game'
#lib/extended_faker/game.rb
Module Faker
class Game < Faker::Base
class << self
def name
fetch('game.name')
end
end
end
end
#config/locals/faker_en.yml
en:
faker:
game:
name: ["a", "b", "c"]
#config/application.rb
...
config.autoload_paths += Dir["#{config.root}/lib/**/"]
...
then when i run it in a rails console i get the following
Loading development environment (Rails 3.2.9)
1.9.3p194 :001 > Faker::Game.name
LoadError: Expected /home/enermis/School/Projects/IG/test/lib/extended_faker/game.rb to define Game
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:503:in `load_missing_constant'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:192:in `block in const_missing'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `each'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `const_missing'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:514:in `load_missing_constant'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:192:in `block in const_missing'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `each'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `const_missing'
from (irb):1
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
When i change the lib/extended_faker/game.rb file to this
require 'faker'
require 'extended_faker/game'
include 'extended_faker/item'
include 'extended_faker/team'
i get weird behavior in the console
Loading development environment (Rails 3.2.9)
1.9.3p194 :001 > Faker::Game.name
=> "b"
1.9.3p194 :002 > Faker::Game.name
NameError: uninitialized constant Faker::Game
from (irb):2
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
The weird thing i don't understand is that the first time i run the generator, i get a valid result, but the second time around i get an error...
What am i missing?
you shoud put it in
lib/faker/game.rb
starting in the lib directory, rails convention is outermost module name -> in are folder names. Then the actual class/module name is the file name, underscored to the camelcase.
Another example
module Foo
module Bar
class BazParty
def self.hello
puts "hello"
end
end
end
end
would go in lib/foo/bar/baz_party.rb
Related
I am setting up a project and have been trying to make rr mocks work but I am getting nomethod error. I have tried moving the required method as well but nothing works. This is my repo
$ rspec spec/views
F
Failures:
1) home/show.html.haml
Failure/Error: stub(view).user { user }
NoMethodError:
undefined method `stub' for #<RSpec::ExampleGroups::HomeShowHtmlHaml:0x007fc245188f08>
# ./spec/views/home/show.html.haml_spec.rb:6:in `block (2 levels) in <top (required)>'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:63:in `load'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:63:in `kernel_load'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:24:in `run'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/lib/bundler/cli.rb:304:in `exec'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/lib/bundler/cli.rb:11:in `start'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/exe/bundle:27:in `block in <top (required)>'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/lib/bundler/friendly_errors.rb:98:in `with_friendly_errors'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/exe/bundle:19:in `<top (required)>'
I have tried whatever was mentioned here rr
# Gemfile
group :test do
gem 'rr', require: false
end
# test helper
require File.expand_path('../../config/environment', __FILE__)
require 'your/test/framework' # if you are using something other than MiniTest / Test::Unit
require 'rr'
I got this working finally, I used gem 'rr', '1.1.2' instead of 1.2.0 which got auto installed when running bundler without the gem version.
My Ruby file
require 'puppet'
# ...
require 'googleauth'
module PuppetX
module Puppetlabs
module Google_cloud_dns
class Provider < PuppetX::Puppetlabs::Swagger::Provider
def client
self.class.client
end
My RSpec file
I am using this two alternatively, but none of them working:
require_relative'../../lib/puppet_x/puppetlabs/google_cloud_dns/provider'
File.expand_path("../../lib/puppet_x/puppetlabs/google_cloud_dns/provider.rb",File.dirname(__FILE__)))
require 'spec_helper'
require_relative '../../lib/puppet_x/puppetlabs/google_cloud_dns/provider'
File.expand_path("../../lib/puppet_x/puppetlabs/google_cloud_dns/provider.rb", File.dirname(__FILE__))
describe Provider do
it "checks function" do
provide = Provider.new
provide.should respond_to :client
end
end
The error which I am getting
rspec Mymodule/gc_dns/puppet-google_cloud_dns/spec/classes/provider_spec.rb --format documentation/home/vivekkumarmishra17/Mymodule/gc_dns/puppet-google_cloud_dns/spec/classes/provider_spec.rb:19:in `<top (required)>': uninitialized constant Provider (NameError)
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1361:in `load'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1361:in `block in load_spec_files'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1359:in `each'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1359:in `load_spec_files'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:106:in `setup'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:92:in `run'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:78:in `run'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:45:in `invoke'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/exe/rspec:4:in `<top (required)>'
from /usr/local/bin/rspec:23:in `load'
from /usr/local/bin/rspec:23:in `<main>'
Can anyone tell what I am doing wrong?
undefined local variable or method `login_user'
Why are my controllers not getting the extended ControllerMacros ???
Commit in question: https://github.com/shadowbq/cartoque/commit/2fba99c6eac5f9f2f0da2dd464f475cae0bae520
<snip>
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
<...snip>
<snip>
# include devise helpers in controller specs
config.include Devise::TestHelpers, :type => :controller
config.extend ControllerMacros, :type => :controller
<...snip>
/usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/example_group.rb:675:in `method_missing': undefined local variable or method `login_user' for RSpec::ExampleGroups::BackupExclusionsController:Class (NameError)
from /home/shadowbq/sandbox/cartoque/spec/controllers/backup_exlusions_controller_spec.rb:5:in `block in <top (required)>'
<snip>
module ControllerMacros
def login_admin
before(:each) do
#request.env["devise.mapping"] = Devise.mappings[:admin]
#user = FactoryGirl.create(:admin)
sign_in #user
end
end
def login_user
before(:each) do
#request.env["devise.mapping"] = Devise.mappings[:user]
#user = FactoryGirl.create(:user)
sign_in #user
end
end
end
<...snip>
Here is full backtrace as well..
/usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/example_group.rb:675:in `method_missing': undefined local variable or method `login_user' for RSpec::ExampleGroups::BackupExclusionsController:Class (NameError)
from /home/shadowbq/sandbox/cartoque/spec/controllers/backup_exlusions_controller_spec.rb:5:in `block in <top (required)>'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/example_group.rb:385:in `module_exec'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/example_group.rb:385:in `subclass'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/example_group.rb:255:in `block in define_example_group_method'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/dsl.rb:43:in `block in expose_example_group_alias'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/dsl.rb:82:in `block (2 levels) in expose_example_group_alias_globally'
from /home/shadowbq/sandbox/cartoque/spec/controllers/backup_exlusions_controller_spec.rb:3:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1361:in `load'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1361:in `block in load_spec_files'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1359:in `each'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1359:in `load_spec_files'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:102:in `setup'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:88:in `run'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:73:in `run'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:41:in `invoke'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/exe/rspec:4:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.2.3/bin/rspec:23:in `load'
from /usr/local/rvm/gems/ruby-2.2.3/bin/rspec:23:in `<main>'
from /usr/local/rvm/gems/ruby-2.2.3/bin/ruby_executable_hooks:15:in `eval'
from /usr/local/rvm/gems/ruby-2.2.3/bin/ruby_executable_hooks:15:in `<main>'
More file snippets
root#ubuntu:/home/shadowbq/sandbox/cartoque# head -15 spec/controllers/backup_exlusions_controller_spec.rb
require 'spec_helper'
describe BackupExclusionsController do
login_user
before do
#backup_exclusion = BackupExclusion.create
end
it "gets index" do
get :index
assert_response :success
assert_not_nil assigns(:backup_exclusions)
end
https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-%28and-RSpec%29
In rspec-rails 3, specs no longer have their type set automatically based on path, so your controller specs no longer have type :controller. You can either:
reneebable this with the config.infer_spec_type_from_file_location! option
explicitly tag your controller specs with type: :controller
I am using Mocha for testing.
2.1.5 :047 > require 'mocha'
=> false
AFAIK it means mocha is already loaded and should be fine. Anyway, when mocking or stubbing:
2.1.5 :048 > mock
NameError: undefined local variable or method `mock' for main:Object
from (irb):48
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/console.rb:90:in `start'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/console.rb:9:in `start'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/commands_tasks.rb:69:in `console'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands.rb:17:in `<top (required)>'
from ./test/dummy/bin/rails:4:in `require'
from ./test/dummy/bin/rails:4:in `<main>'
2.1.5 :049 > Github.expects :repos
NoMethodError: undefined method `expects' for Github:Module
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/github_api-0.12.3/lib/github_api.rb:56:in `method_missing'
from (irb):49
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/console.rb:90:in `start'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/console.rb:9:in `start'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/commands_tasks.rb:69:in `console'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands.rb:17:in `<top (required)>'
from ./test/dummy/bin/rails:4:in `require'
from ./test/dummy/bin/rails:4:in `<main>'
2.1.5 :051 > stub
NameError: undefined local variable or method `stub' for main:Object
from (irb):51
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/console.rb:90:in `start'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/console.rb:9:in `start'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/commands_tasks.rb:69:in `console'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands.rb:17:in `<top (required)>'
from ./test/dummy/bin/rails:4:in `require'
from ./test/dummy/bin/rails:4:in `<main>'
Does mocking should work in IRB or not?
I had the same error before I include Mocha::API. This will include appropriate Mocha's methods to your Object, Module, Class classes and add missed functionality to your irb session:
$ irb
Welcome to IRB. You are using ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-linux]. Have fun ;)
>> require 'mocha/api' #=> true
>> include Mocha::API #=> Object
>> mock #=> #<Mock:0x2da1ce8>
>> stub #=> #<Mock:0x2d6a680>
>> Object.expects :something #=> #<Expectation:0x256e878 expected exactly once, not yet invoked: Object.something(any_parameters) >
So this is kind of baffling me as I can't quite figure out why it's happening. This only happens on my laptop (Ubuntu 11.04), and not elsewhere. I just seem to have something weird with the setup on this one computer.
I keep getting the following error when I run my specs:
be rake spec
Gives me:
NoMethodError: undefined method `belong_to' for #<RSpec::Core::ExampleGroup::Nested_4:0xb4eb2e4>
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-expectations-2.6.0/lib/rspec/matchers/method_missing.rb:9:in `method_missing'
/home/tom/work/ruby/litdistco-sales/spec/models/sales_item_spec.rb:5:in `block (2 levels) in <top (required)>'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:48:in `instance_eval'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:48:in `block in run'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:107:in `with_around_hooks'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:45:in `run'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:294:in `block in run_examples'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:290:in `map'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:290:in `run_examples'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:262:in `run'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:24:in `block (2 levels) in run'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:24:in `map'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:24:in `block in run'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/reporter.rb:12:in `report'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:21:in `run'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:80:in `run_in_process'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:69:in `run'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:11:in `block in autorun'
Here are the relevant lines from my spec file that generates the complaint:
describe SalesItem do
it { should belong_to(:publisher) }
it { should belong_to(:invoice) }
I'm running Rails 3.1.0. Here is ruby -v:
ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux]
Any tips /thoughts/ideas recommendations greatly appreciated.
Try adding this in your rails_helper.rb
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
RSpec core does not have such matcher. It looks like shoulda-matchers. Just make sure that it's installed and loaded in your spec_helper
I was having a hard time with this for awhile and then changed my spec from:
describe ModelName do
it { should belong_to(:model)}
end
to:
RSpec.describe ModelName, type: :model do
it { should belong_to(:model)}
end
and it suddenly worked
You can write specs like this
describe SalesItem do
describe "Associations" do
it "belongs_to publisher" do
assc = described_class.reflect_on_association(:publisher)
expect(assc.macro).to eq :belongs_to
end
end
end