So following the Hartl sample app tutorials, and having a bit of trouble getting some tests to past regarding the relationship spec.
in my spec file I have
describe "follow methods" do
before(:each) do
#follower = User.create!( :name => "Example User",
:email => "user#example.com",
:password => "foobar",
:password_confirmation => "foobar"
)
#followed = User.create!( :name => "Example User",
:email => "user_five#example.com",
:password => "foobar",
:password_confirmation => "foobar"
)
#attr = { :followed_id => #followed.id }
#relationship = #follower.relationships.create!(#attr)
end
I know it's not the same as in the book however I'm not using FactoryGirl.
When using this block for tests I am getting an error with my #attr which reads;
Failure/Error: #attr = { :followed_id => #followed.id }
NoMethodError:
undefined method `id' for #<Hash:0x00000108d0b2a0>
However, when I run this block in my console it works just fine. The id is set according to the #attr, and I can call .id on #followed.
Any help would be much appreciated as always!
According to your error, the method id does not exist for a Hash.
To access that, you can use this:
#attr = { :followed_id => #followed['id'] }
The problem was you were trying to do #followed.id
Related
I want to add a birthday date to my user model, this is how I implemented it:
user.rb
validates_date :birthday_date
user_spec.rb
describe User do
before(:each) do
#attr = {
:birthday_date => Date.new(1992, 8, 27),
:nom => "Utilisateur",
:email => "user#example.com",
:password => "foobar",
:password_confirmation => "foobar"
}
end
it "..." do
User.create!(#attr)
end
it "must have a correct date" do
bad_guy = User.new(#attr.merge(:birthday_date => ""))
bad_guy.should_not be_valid
end
But when I want to test it, I have got this error :
Failure/Error: #follower = Factory(:user)
ActiveRecord::RecordInvalid:
Validation failed: Birthday date is not a valid date
Why is my date not accepted?
I am trying to populate my db using rake db:populate. I am on chapter 10.3.2 on michael hartl's book.
Even though I don't get any error messages the DB doesn't seem to be populating.
This is the sample_data.rake file I created:
namespace :db do desc "Fill database with sample data" task populate: :environment do
User.create!(:name => "Example User",
:email => "example#railstutorial.org",
:password => "foobar",
:password_confirmation => "foobar")
99.times do |n|
name = Faker::Name.name
email = "example-#{n+1}#railstutorial.org"
password = "password"
User.create!(:name => name,
:email => email,
:password => password,
:password_confirmation => password)
end
end
end
Stopping your server before resetting and repopulating your db should work.
At present, the following works when I call it from my Rspec tests:
def login_as(role)
#role = Role.create! :name => role
#virginia = User.create!(
:username => "Virginia",
:password => "password",
:password_confirmation => "password",
:email => "example#example.com")
#assignment = Assignment.create! user_id: #virginia.id, role_id: #role.id
visit login_path
fill_in "user_session_username", :with => #virginia.username
fill_in "user_session_password", :with => #virginia.password
click_on "submit_user_session"
end
Given that I've tested the interface already, I thought it might speed things up to not hit the interface hundreds of times just because many of my examples only make sense when a user has a role and is logged in. So, I tried this:
def login_as(role)
#role = Role.create! :name => role
#virginia = User.create!(
:username => "Virginia",
:password => "password",
:password_confirmation => "password",
:email => "example#example.com")
#assignment = Assignment.create! user_id: #virginia.id, role_id: #role.id
#user_session = UserSession.create! :username => #virginia.username, :password => #virginia.password
end
However, that version is not logging me in. Any thoughts?
To be clear, I'm not asking how to test declarative_authorization. I'm asking how to quickly login when I'm testing something else. For example, I use login_as(role) to test product#delete (an action only accessible to admins) like this:
login_as "admin"
visit product_path(#search)
click_link "delete_product"
page.current_path.should == products_path
page.should_not have_content "Retail Site Search"
When I run the following spec:
require 'spec_helper'
describe User do
before :each do
#user = User.new :email => "foo#bar.com", :password => "foobar", :password_confirmation => "foobar"
end
it "should be valid" do
#user.should be_valid
end
end
I get this error:
1) User should be valid
Failure/Error: #user = User.new :email => "foo#bar.com", :password => "foobar", :password_confirmation => "foobar"
ActiveRecord::UnknownAttributeError:
unknown attribute: email
# ./spec/models/user_spec.rb:5:in `new'
However, when I go in to console and run
user = User.new :email => "foo#bar.com", :password => "foobar", :password_confirmation => "foobar"
user.valid?
It returns true. For some reason, in my test, I am unable to create a User instance, saying that the email attribute is inaccessible.
Console uses the development database, but specs use the test database. Make sure email is defined in both.
I am using faker to generate sample data. I have this as follows:
require 'faker'
namespace :db do
desc "Fill database with sample data"
task :populate => :environment do
Rake::Task['db:reset'].invoke
User.create!(:name => "rails",
:email => "example#railstutorial.org",
:password => "foobar",
:password_confirmation => "foobar")
99.times do |n|
#name = Faker::Name.name
name = "rails#{n+1}"
email = "example-#{n+1}#railstutorial.org"
password = "password"
user = User.create!(:name => name,
:email => email,
:password => password,
:password_confirmation => password)
end
end
end
The problem is that I have a couple of after_save callbacks that are not being called when the User is created. Why is that? Thanks
The methods:
after_save :create_profile
def create_profile
self.build_profile()
end
In all my reading, it seems that save! bypasses any custom before_save, on_save or after_save filters you have defined. The source code for create! reveals that it invokes save!. Unless you absolutely NEED the bang version, why are you using it? Try removing all your bang methods and just invoking the non-bang versions:
[1..100].each do |n|
name = "rails#{n+1}"
email = "example-#{n+1}#railstutorial.org"
password = "password"
user = User.new(:name => name, :email => email, :password => password, :password_confirmation => password)
if !user.save
puts "There was an error while making #{user.inspect}"
end
end