I'm Trying to build a simple Blog using rails 4 and For authentication I'm using Devise.
The blog is simple and it will only have one registered user which is the admin. so the registration page should be available when there are no users in the database else it will redirect to the home page.
I'm trying to learn testing in the process. So I started testing the RegistrationContrroler However it doesn't seem to pass.
class RegistrationsController < Devise::RegistrationsController
before_action :one_user_registered?, only: [:new, :create]
protected
def one_user_registered?
if ((User.count == 1) & (user_signed_in?))
redirect_to root_path
elsif User.count == 1
redirect_to new_user_session_path
end
end
end
Specs:
RSpec.describe RegistrationsController, type: :controller do
context "Admin already registred" do
it "cannot create new user" do
#user_attr = FactoryGirl.attributes_for(:user)
# Create Admin
User.create!(#user_attr)
#request.env["devise.mapping"] = Devise.mappings[:user]
post :create, user: #user_attr
User.count should eq_to(1)
end
end
end
spec/factories.rb :
FactoryGirl.define do
factory :user do
sequence(:email) { |n| "user#{n}#example.com" }
password "secure"
end
factory :post do
sequence(:id){ |n| n }
sequence(:title) { |n| "Post title #{n}" }
body "Post body"
sequence(:slug) { |n| "Post title #{n}" }
end
end
routes.rb
Rails.application.routes.draw do
devise_for :users, controllers: { registrations: "registrations" }
authenticated :user do
resources :posts
end
unauthenticated :user do
resources :posts, only: [:show, :index]
end
root 'posts#index'
end
Stacktrace :
F
Failures:
1) RegistrationsController Admin already registred cannot create new user
Failure/Error: User.count should eq_to(1)
expected: 1
got: #<RegistrationsController:0x00000003a413b8 #_action_has_layout=true, #_routes=nil, #_headers={"Conten...enticatable]>, #req touest_format=:html, #marked_for_same_origin_verification=false, #current_user=nil>
(compared using ==)
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-support-3.5.0/lib/rspec/support.rb:87:in `block in <module:Support>'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-support-3.5.0/lib/rspec/support.rb:96:in `notify_failure'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-expectations-3.5.0/lib/rspec/expectations/fail_with.rb:27:in `fail_with'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-expectations-3.5.0/lib/rspec/expectations/handler.rb:38:in `handle_failure'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-expectations-3.5.0/lib/rspec/expectations/handler.rb:50:in `block in handle_matcher'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-expectations-3.5.0/lib/rspec/expectations/handler.rb:27:in `with_matcher'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-expectations-3.5.0/lib/rspec/expectations/handler.rb:48:in `handle_matcher'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/memoized_helpers.rb:81:in `should'
# ./spec/controllers/registrations_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:252:in `instance_exec'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:252:in `block in run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:494:in `block in with_around_and_singleton_context_hooks'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:451:in `block in with_around_example_hooks'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:471:in `block in run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:611:in `block in run_around_example_hooks_for'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:336:in `call'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-rails-3.5.1/lib/rspec/rails/example/controller_example_group.rb:191:in `block (2 levels) in <module:ControllerExampleGroup>'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:441:in `instance_exec'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:441:in `instance_exec'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:382:in `execute_with'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:613:in `block (2 levels) in run_around_example_hooks_for'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:336:in `call'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-rails-3.5.1/lib/rspec/rails/adapters.rb:127:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:441:in `instance_exec'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:441:in `instance_exec'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:382:in `execute_with'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:613:in `block (2 levels) in run_around_example_hooks_for'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:336:in `call'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:614:in `run_around_example_hooks_for'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:471:in `run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:451:in `with_around_example_hooks'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:494:in `with_around_and_singleton_context_hooks'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:249:in `run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:627:in `block in run_examples'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:623:in `map'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:623:in `run_examples'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:589:in `run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:590:in `block in run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:590:in `map'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:590:in `run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:113:in `block (3 levels) in run_specs'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:113:in `map'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:113:in `block (2 levels) in run_specs'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/configuration.rb:1836:in `with_suite_hooks'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:112:in `block in run_specs'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/reporter.rb:77:in `report'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:111:in `run_specs'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:87:in `run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:71:in `run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:45:in `invoke'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/exe/rspec:4:in `<top (required)>'
# /home/chaker/.rvm/gems/ruby-2.3.1/bin/rspec:23:in `load'
# /home/chaker/.rvm/gems/ruby-2.3.1/bin/rspec:23:in `<main>'
# /home/chaker/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `eval'
# /home/chaker/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `<main>'
Finished in 0.08188 seconds (files took 1.95 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/controllers/registrations_controller_spec.rb:7 # RegistrationsController Admin already registred cannot create new user
You're missing a dot!
User.count.should eq_to(1)
Adding a should method is the way rspec patches all objects to support the should comparisons.
Preferable would be...
expect(User.count).to eq(1)
The error showed that the should by itself was received by the RegistrationsController, not by the User.count... and of course the RegistrationsController doesn't equal 1.
Related
I am trying to use Faker in FactoryBot in my Rails API tests.
The issue I am having that I keep getting this error:
KeyError:
Trait not registered: "first_name"
So far as I can tell I have configured everything right, based on Faker'd docs at least. So far as I can tell I have two actual traits, valid_user and invalid_user that I set like so...
let(:valid_attributes) { FactoryBot.attributes_for :api_v1_user, :valid_user }
let(:invalid_attributes) { FactoryBot.attributes_for :api_v1_user, :invalid_user }
Which are linked to the :api_v1_user factory.
What have I missed or done wrong?
Full Error:
8) /api/v1/users PATCH /update with invalid parameters renders a JSON response with errors for the api/v1_user
Failure/Error: let(:invalid_attributes) { FactoryBot.attributes_for(:api_v1_user, :invalid_user) }
KeyError:
Trait not registered: "first_name"
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/activesupport-6.0.2.2/lib/active_support/hash_with_indifferent_access.rb:191:in `fetch'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/activesupport-6.0.2.2/lib/active_support/hash_with_indifferent_access.rb:191:in `fetch'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/registry.rb:23:in `find'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/decorator.rb:10:in `method_missing'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/internal.rb:49:in `trait_by_name'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition.rb:114:in `trait_by_name'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition.rb:106:in `block in base_traits'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition.rb:106:in `map'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition.rb:106:in `base_traits'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition.rb:51:in `block in compile'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition.rb:50:in `compile'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition.rb:130:in `aggregate_from_traits_and_self'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition.rb:34:in `to_create'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/trait.rb:17:in `to_create'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition.rb:135:in `map'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition.rb:135:in `aggregate_from_traits_and_self'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition.rb:34:in `to_create'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/definition_hierarchy.rb:16:in `build_from_definition'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/factory.rb:125:in `build_hierarchy'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/factory.rb:88:in `compile'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/factory.rb:32:in `run'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/factory_runner.rb:29:in `block in run'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/activesupport-6.0.2.2/lib/active_support/notifications.rb:182:in `instrument'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/factory_runner.rb:28:in `run'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/factory_bot-5.1.2/lib/factory_bot/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
# ./spec/requests/api/v1/users_spec.rb:6:in `block (2 levels) in <top (required)>'
# ./spec/requests/api/v1/users_spec.rb:92:in `block (4 levels) in <top (required)>'
# ./spec/rails_helper.rb:19:in `block (3 levels) in <top (required)>'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/database_cleaner-1.8.4/lib/database_cleaner/generic/base.rb:16:in `cleaning'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/database_cleaner-1.8.4/lib/database_cleaner/configuration.rb:87:in `block (2 levels) in cleaning'
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/database_cleaner-1.8.4/lib/database_cleaner/configuration.rb:88:in `cleaning'
# ./spec/rails_helper.rb:18:in `block (2 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# KeyError:
# key not found: "first_name"
# /home/etherk1ll/.rvm/gems/ruby-2.7.0/gems/activesupport-6.0.2.2/lib/active_support/hash_with_indifferent_access.rb:191:in `fetch'
spec/factories/api/v1/users.rb
require 'faker'
FactoryBot.define do
factory :api_v1_user, class: 'Api::V1::User' do
trait :valid_user do
first_name { Faker::Name.first_name }
second_name { Faker::Name.second_name }
username { Faker::Internet.username }
email { Faker::Internet.safe_email }
password { Faker::Internet.password(min_length: 8) }
end
trait :invalid_user do
first_name nil
end
end
end
rails_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
RSpec.configure do |config|
config.use_transactional_fixtures = false
DatabaseCleaner.strategy = :truncation
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
config.before(:all) do
DatabaseCleaner.start
end
config.after(:all) do
DatabaseCleaner.clean
end
end
users_spec.rb
Truncated
RSpec.describe "/api/v1/users", type: :request do
let(:valid_attributes) { FactoryBot.attributes_for :api_v1_user, :valid_user }
let(:invalid_attributes) { FactoryBot.attributes_for :api_v1_user, :invalid_user }
let(:valid_headers) {
{}
}
describe "PATCH /update" do
context "with invalid parameters" do
it "renders a JSON response with errors for the api/v1_user" do
user = Api::V1::User.create! valid_attributes
patch api_v1_user_url(user),
params: { api_v1_user: invalid_attributes }, headers: valid_headers, as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to eq("application/json")
end
end
end
end
Try wrapping the nil in brackets. FactoryBot eliminated support for static attributes in version 5, so if you're using a newer version, that'll be your problem. Brackets are now required.
trait :invalid_user do
first_name { nil }
end
So I was testing emails with RSpec and the email_spec gem and I keep coming across this error that leaves me with no real clue as to what is causing it.
I am very new to RSpec and testing in general. When I googled this the error I found one post that was similar to this error as well as a brief documentation page on the error (which came off to me as very ambiguous).
I believe the reason is due to a possibly outdated gem or method from email_spec, but I am just speculating (there's a pun for your time).
This is the error I keep receiving (I redacted my email for this post):
1) UserMailer it sends an activation email when the user signs up should be delivered from XXXXXXXX#gmail.com
Failure/Error: it { should deliver_from "XXXXXXXX#gmail.com" }
NoMethodError:
undefined method `addrs' for #<Mail::UnstructuredField:0x007fc5c680bf18>
# ./spec/mailers/user_mailer_spec.rb:12:in `block (3 levels) in <top (required)>'
Few things to point out:
I am not directly referencing the addrs method in any of my tests (so I believe)
I have included Email::Helpers and Email::Matchers in my config/test.rb file.
Here is my UserMailer test:
require "spec_helper"
require "email_spec"
describe UserMailer do
describe "it sends an activation email when the user signs up" do
user = FactoryGirl.build(:user)
subject { UserMailer.activate_account(user) }
it { should deliver_to user.email }
# Error seems to be occurring from the "deliver_from" method
it { should deliver_from "XXXXXXX2#gmail.com" }
it { should have_subject "Please confirm to activate your account." }
it { should have_body_text "Hi, #{user.first_name}" }
it { should have_body_text "Confirm Your Account" }
end
end
I would just like to be helped and pointed in the right direction as to what is causing this issue so I may take the appropriate steps to resolve it.
EDIT
Added the backtrace as requested.
# /Library/Ruby/Gems/2.0.0/gems/mail-2.6.3/lib/mail/field.rb:189:in `method_missing'
# /Library/Ruby/Gems/2.0.0/gems/email_spec-1.6.0/lib/email_spec/matchers.rb:92:in `block in matches?'
# /Library/Ruby/Gems/2.0.0/gems/email_spec-1.6.0/lib/email_spec/matchers.rb:6:in `address_array'
# /Library/Ruby/Gems/2.0.0/gems/email_spec-1.6.0/lib/email_spec/matchers.rb:92:in `matches?'
# /Library/Ruby/Gems/2.0.0/gems/rspec-expectations-2.99.2/lib/rspec/expectations/handler.rb:24:in `handle_matcher'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/memoized_helpers.rb:77:in `should'
# ./spec/mailers/user_mailer_spec.rb:12:in `block (3 levels) in <top (required)>'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example.rb:116:in `block in run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example.rb:178:in `call'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example.rb:178:in `run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example.rb:241:in `instance_eval_with_args'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/hooks.rb:106:in `block (2 levels) in run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/hooks.rb:108:in `call'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/hooks.rb:108:in `run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/hooks.rb:446:in `run_hook'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:463:in `run_around_each_hooks'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example.rb:250:in `with_around_each_hooks'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example.rb:113:in `run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:515:in `block in run_examples'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:511:in `map'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:511:in `run_examples'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:496:in `run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:497:in `block in run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:497:in `map'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:497:in `run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/command_line.rb:24:in `block (2 levels) in run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/command_line.rb:24:in `map'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/command_line.rb:24:in `block in run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/reporter.rb:58:in `report'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/command_line.rb:21:in `run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/runner.rb:103:in `run'
# /Library/Ruby/Gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/runner.rb:17:in `block in autorun'
Excellent! I was able to figure it out.
Issues was in my actual UserMailer class (in /app/mailers/user_mailer.rb).
The default from: statement at the top did not match with my tests.
class UserMailer < ApplicationMailer
default from: "XXXXXXXX2#gmail.com" # <= This was the culprit
def activate_account(user)
#user = user
mail(:to => #user.email, :subject => "Please confirm to activate your account.")
end
end
I recently ran into this and my problem was that i hadn't provided a proper email address in the :to field
By mistake I had -
mail(to: #user)
Instead of
mail(to: #user.email)
This test gives no reason as to why it fails:
it "saves the new job in the database" do
expect{
post :create, booking: FactoryGirl.attributes_for(:booking)
}.to change(Booking,:count).by(1)
end
The test seems simple enough, and the functionality works in the site.
UPD For reference, here's the controller it's testing:
def create
#booking = Booking.new(booking_params)
#booking.save
respond_with(#booking)
end
And the factory:
FactoryGirl.define do
factory :booking do
employer
vehicle
job_type
location
start "2015-04-23 09:00"
ending "2015-04-23 15:00"
end
end
UPD Here's the rspec log, with -b:
BookingsController
as employer
GET #new
renders the :new template (PENDING: Not yet implemented)
assigns a new job to #job (PENDING: Not yet implemented)
GET #create
with invalid attributes
re-renders the :new template (PENDING: Not yet implemented)
does not save in the database (PENDING: Not yet implemented)
with valid attributes
redirects to the show page (PENDING: Not yet implemented)
in populate_times
saves the new job in the database (FAILED - 1)
GET #create
with invalid attributes
re-renders the :new template (PENDING: Not yet implemented)
does not save in the database (PENDING: Not yet implemented)
with valid attributes
redirects to the show page (PENDING: Not yet implemented)
saves the new job in the database (PENDING: Not yet implemented)
GET new
renders the new template
GET #show
renders the :show template (PENDING: Not yet implemented)
assigns the requested booking to #booking (PENDING: Not yet implemented)
as visitor
GET new
redirects to new_employer_path
Pending: (Failures listed here are expected and do not affect your suite's status)
1) BookingsController as employer GET #new renders the :new template
# Not yet implemented
# ./spec/controllers/booking_controller_spec.rb:60
2) BookingsController as employer GET #new assigns a new job to #job
# Not yet implemented
# ./spec/controllers/booking_controller_spec.rb:59
3) BookingsController as employer GET #create with invalid attributes re-renders the :new template
# Not yet implemented
# ./spec/controllers/booking_controller_spec.rb:38
4) BookingsController as employer GET #create with invalid attributes does not save in the database
# Not yet implemented
# ./spec/controllers/booking_controller_spec.rb:37
5) BookingsController as employer GET #create with valid attributes redirects to the show page
# Not yet implemented
# ./spec/controllers/booking_controller_spec.rb:33
6) BookingsController as employer GET #create with invalid attributes re-renders the :new template
# Not yet implemented
# ./spec/controllers/booking_controller_spec.rb:71
7) BookingsController as employer GET #create with invalid attributes does not save in the database
# Not yet implemented
# ./spec/controllers/booking_controller_spec.rb:70
8) BookingsController as employer GET #create with valid attributes redirects to the show page
# Not yet implemented
# ./spec/controllers/booking_controller_spec.rb:66
9) BookingsController as employer GET #create with valid attributes saves the new job in the database
# Not yet implemented
# ./spec/controllers/booking_controller_spec.rb:65
10) BookingsController as employer GET #show renders the :show template
# Not yet implemented
# ./spec/controllers/booking_controller_spec.rb:55
11) BookingsController as employer GET #show assigns the requested booking to #booking
# Not yet implemented
# ./spec/controllers/booking_controller_spec.rb:43
Failures:
1) BookingsController as employer GET #create with valid attributes saves the new job in the database
Failure/Error: expect{
expected #count to have changed by 1, but was changed by 0
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-expectations-3.2.1/lib/rspec/expectations/fail_with.rb:29:in `fail_with'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-expectations-3.2.1/lib/rspec/expectations/handler.rb:40:in `handle_failure'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-expectations-3.2.1/lib/rspec/expectations/handler.rb:50:in `block in handle_matcher'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-expectations-3.2.1/lib/rspec/expectations/handler.rb:27:in `with_matcher'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-expectations-3.2.1/lib/rspec/expectations/handler.rb:48:in `handle_matcher'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-expectations-3.2.1/lib/rspec/expectations/expectation_target.rb:54:in `to'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-expectations-3.2.1/lib/rspec/expectations/expectation_target.rb:87:in `to'
# ./spec/controllers/booking_controller_spec.rb:28:in `block (5 levels) in <top (required)>'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:177:in `instance_exec'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:177:in `block in run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:385:in `block in with_around_and_singleton_context_hooks'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:343:in `block in with_around_example_hooks'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:474:in `block in run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:614:in `block in run_around_example_hooks_for'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:in `call'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:in `call'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-rails-3.2.1/lib/rspec/rails/example/controller_example_group.rb:179:in `block (2 levels) in <module:ControllerExampleGroup>'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:333:in `instance_exec'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:333:in `instance_exec'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:387:in `execute_with'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:616:in `block (2 levels) in run_around_example_hooks_for'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:in `call'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:in `call'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-rails-3.2.1/lib/rspec/rails/adapters.rb:113:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:333:in `instance_exec'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:333:in `instance_exec'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:387:in `execute_with'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:616:in `block (2 levels) in run_around_example_hooks_for'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:in `call'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:in `call'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:617:in `run_around_example_hooks_for'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:474:in `run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:343:in `with_around_example_hooks'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:385:in `with_around_and_singleton_context_hooks'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:174:in `run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:549:in `block in run_examples'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:545:in `map'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:545:in `run_examples'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:512:in `run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:in `block in run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:in `map'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:in `run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:in `block in run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:in `map'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:in `run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:in `block in run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:in `map'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:in `run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:110:in `block (3 levels) in run_specs'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:110:in `map'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:110:in `block (2 levels) in run_specs'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1526:in `with_suite_hooks'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:109:in `block in run_specs'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/reporter.rb:62:in `report'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:108:in `run_specs'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:86:in `run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:70:in `run'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:38:in `invoke'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.2.3/exe/rspec:4:in `<top (required)>'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/bin/rspec:23:in `load'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/bin/rspec:23:in `<main>'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `eval'
# /Users/Will/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `<main>'
I can't add comments cause of my reputation ... but maybe u shoud try to run rspec with -b option to see full backtraces. And detect the error
This is what my spec file looks like: spec/api/v1/projects_spec.rb
require "spec_helper"
describe "/api/v1/projects", :type => :api do
context "projects viewable by this user" do
it "JSON" do
end
end
end
It contained a lot more but I deleted a lot of lines in an unsuccessful attempt to find the error which goes like this:
Failure/Error: Unable to find matching line from backtrace
NoMethodError: undefined method `env' for nil:NilClass
This piece of code is from the "Rails 3 in Action" by Ryan Bigg. The only other file it includes is: spec/support/api/helper.rb
module ApiHelper
include Rack::Test::Methods
def app
Rails.application
end
end
RSpec.configure do |c|
c.include ApiHelper, :type => :api
end
I've been trying to google the error for the past 1 hr and the closest thing that I found was this and as a result I deleted the code which used Devise::Test_Helpers. Sadly, its still not working.
Any help would be appreciated. Thank you so much.
Stack Trace
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/devise-2.0.4/lib/devise/test_helpers.rb:24:in `setup_controller_for_warden'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-rails-2.8.1/lib/rspec/rails/adapters.rb:15:in `block (2 levels) in setup'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:35:in `instance_eval'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:35:in `run_in'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:70:in `block in run_all'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:70:in `each'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:70:in `run_all'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/hooks.rb:368:in `run_hook'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:292:in `block in run_before_each_hooks'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:292:in `each'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:292:in `run_before_each_hooks'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example.rb:217:in `run_before_each'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example.rb:79:in `block in run'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example.rb:173:in `with_around_hooks'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example.rb:77:in `run'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:355:in `block in run_examples'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:351:in `map'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:351:in `run_examples'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:337:in `run'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:338:in `block in run'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:338:in `map'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:338:in `run'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:28:in `map'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:28:in `block in run'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/reporter.rb:34:in `report'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:25:in `run'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:80:in `run_in_process'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:69:in `run'
# /home/prakhar/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:10:in `block in autorun'
You're still including Devise::TestHelpers somewhere. Check and see what "spec_helper" is loading.
Instead of just including the Devise Test Helper, you can specify it's type, so that it's not included in routing spec:
config.include Devise::TestHelpers, :type => :helper
config.include Devise::TestHelpers, :type => :controller
Model:
class Plan
include Mongoid::Document
# Fields
field :name, type: String
# Relationships
references_many :sub_plans,
:autosave => true,
:class_name => 'Plan',
:inverse_class_name => 'Plan',
:inverse_of => :super_plans
references_many :super_plans,
:class_name => 'Plan',
:inverse_class_name => 'Plan',
:inverse_of => :sub_plans
accepts_nested_attributes_for :sub_plans
# Validations
validates_presence_of :name
end
Test:
describe Plan do
it "should always have a name" do
plan = Plan.new
plan.save.should == false
plan[:name] = "World Domination"
plan.save.should == true
end
it "should allow nested plan creation" do
plan = Plan.new(:name => "World Domination", :sub_plans_attributes => {
:name => "Get $50b",
:sub_plans_attributes => {
:name => "Invent"
}
});
plan.sub_plans.count.should == 1
plan.sub_plans.first.name.should == "Get $50b"
plan.sub_plans.first.sub_plans.count.should == 1
plan.sub_plans.first.sub_plans.first.name.should == "Invent"
end
end
Output with -b:
Running spec/models/plan_spec.rb
.F
Failures:
1) Plan should allow nested plan creation
Failure/Error: plan = Plan.new(:name => "World Domination", :sub_plans_attributes => {
TypeError:
can't convert Symbol into String
# /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/relations/builders/nested_attributes/many.rb:67:in `delete'
# /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/relations/builders/nested_attributes/many.rb:67:in `destroyable?'
# /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/relations/builders/nested_attributes/many.rb:103:in `process'
# /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/relations/builders/nested_attributes/many.rb:30:in `block in build'
# /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/relations/builders/nested_attributes/many.rb:26:in `each'
# /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/relations/builders/nested_attributes/many.rb:26:in `build'
# /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/nested_attributes.rb:47:in `block (3 levels) in accepts_nested_attributes_for'
# /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/attributes.rb:193:in `_assigning'
# /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/nested_attributes.rb:45:in `block (2 levels) in accepts_nested_attributes_for'
# /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/attributes/processing.rb:111:in `block in process_nested'
# /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/attributes/processing.rb:110:in `each_pair'
# /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/attributes/processing.rb:110:in `process_nested'
# /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/attributes/processing.rb:122:in `process_pending'
# /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/attributes/processing.rb:29:in `process'
# /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/document.rb:131:in `block in initialize'
# /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/relations/builders.rb:47:in `_building'
# /usr/lib/ruby/gems/1.9.1/gems/mongoid-2.3.1/lib/mongoid/document.rb:127:in `initialize'
# ./spec/models/plan_spec.rb:11:in `new'
# ./spec/models/plan_spec.rb:11:in `block (2 levels) in <top (required)>'
# /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:48:in `instance_eval'
# /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:48:in `block in run'
# /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:107:in `with_around_hooks'
# /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:45:in `run'
# /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:294:in `block in run_examples'
# /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:290:in `map'
# /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:290:in `run_examples'
# /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:262:in `run'
# /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:24:in `block (2 levels) in run'
# /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:24:in `map'
# /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:24:in `block in run'
# /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/reporter.rb:12:in `report'
# /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:21:in `run'
# /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/monkey/spork/test_framework/rspec.rb:5:in `run_tests'
# /usr/lib/ruby/gems/1.9.1/gems/spork-0.9.0.rc9/lib/spork/run_strategy/forking.rb:13:in `block in run'
# /usr/lib/ruby/gems/1.9.1/gems/spork-0.9.0.rc9/lib/spork/forker.rb:21:in `block in initialize'
# /usr/lib/ruby/gems/1.9.1/gems/spork-0.9.0.rc9/lib/spork/forker.rb:18:in `fork'
# /usr/lib/ruby/gems/1.9.1/gems/spork-0.9.0.rc9/lib/spork/forker.rb:18:in `initialize'
# /usr/lib/ruby/gems/1.9.1/gems/spork-0.9.0.rc9/lib/spork/run_strategy/forking.rb:9:in `new'
# /usr/lib/ruby/gems/1.9.1/gems/spork-0.9.0.rc9/lib/spork/run_strategy/forking.rb:9:in `run'
# /usr/lib/ruby/gems/1.9.1/gems/spork-0.9.0.rc9/lib/spork/server.rb:48:in `run'
# /usr/lib/ruby/1.9.1/drb/drb.rb:1558:in `perform_without_block'
# /usr/lib/ruby/1.9.1/drb/drb.rb:1518:in `perform'
# /usr/lib/ruby/1.9.1/drb/drb.rb:1592:in `block (2 levels) in main_loop'
# /usr/lib/ruby/1.9.1/drb/drb.rb:1588:in `loop'
# /usr/lib/ruby/1.9.1/drb/drb.rb:1588:in `block in main_loop'
Finished in 0.00917 seconds
2 examples, 1 failure
Failed examples:
rspec ./spec/models/plan_spec.rb:10 # Plan should allow nested plan creation
Why am I getting this error, and how can I get my many-many self reference working? I'm using Mongoid version 2.3.1 and Rails version 3.1.1. Thanks
After several hours spent trying several different combinations and finally giving up and sleeping on it, I have finally got it to work! Model:
class Plan
include Mongoid::Document
# Fields
field :name, type: String
# Relationships
has_and_belongs_to_many :subplans, :inverse_of => :parents, :class_name => 'Plan', :autosave => true
has_and_belongs_to_many :parents, :inverse_of => :subplans, :class_name => 'Plan', :autosave => true
accepts_nested_attributes_for :subplans
# Validations
validates_presence_of :name
end
Tests:
describe Plan do
it "should always have a name" do
plan = Plan.new
plan.save.should == false
plan[:name] = "World Domination"
plan.save.should == true
end
it "should have a nested relationship" do
root = Plan.create name: "Parent Plan"
child1 = root.subplans.create name: 'Child Plan #1'
child2 = Plan.create name: "Child Plan #2"
root.subplans << child2
root.subplans.size.should == 2
child1.parents.size.should == 1
child1.parents.first.should == root
child2.parents.size.should == 1
child2.parents.first.should == root
end
it "should accept nested attributes for subplans" do
plan = Plan.new :name => "root",
:subplans_attributes => [{:name => "child"}]
plan.save; plan.reload
plan.subplans.size.should == 1
plan.subplans.first.name.should == "child"
end
end