How to configure Cucumber in Rails 4 - ruby-on-rails

I'm trying to use Cucumber in a Rails 4. I've added the Cucumber-Rails gem, followed the steps in the instructions but when I wrote step definitions like so:
When(/^I submit a sign up with the following:$/) do |table|
user = User.create({
first_name: 'Name',
last_name: 'Last',
email: 'email#example.com',
domain: 'example.com',
password: 'foobar',
password_confirmation: 'foobar'
})
end
I get the following error: uninitialized constant User (NameError)
./features/step_definitions/users/sign_up_steps.rb:2:in/^I submit a sign up with the following:$/'
features/users/sign_up.feature:4:in When I submit a sign up with the following:'
What am I missing?

Please check , it's looking like you have missed the User model

User model is missing for which you are running tests.

Related

"Email has already been taken" error on db:seed

I am running through Hartl's Rails tutorial, but when I try to seed Microposts I am getting the error: "ActiveRecord::RecordInvalid: Validation failed: Email has already been taken"
I did db:migrate:reset followed by db:seed which throws up the error. Is there something wrong with what I am trying to seed?
User.create!(name: "Example User",
email: "example#railstutorial.org",
password: "foobar",
password_confirmation: "foobar",
admin: true,
activated: true,
activated_at: Time.zone.now)
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,
activated: true,
activated_at: Time.zone.now)
end
users = User.order(:created_at).take(6)
50.times do
content = Faker::Lorem.sentence(word_count: 5)
users.each { |user| user.microposts.create!(content: content) }
end
You have probably already created some records in the database, which invokes validation. To check this, run the console rails c and then type User.first.
If you have some records run rake db:reset to reset database.
If you want to be able to run the seed multiple times, write
User.find_or_create_by(email: email) do |u|
u.name: name,
u.password: password,
u.password_confirmation: password,
u.activated: true,
u.activated_at: Time.zone.now
end
instead your code.
Instead of create! user create so if the email address is already taken, the record would not be created and the script gracefully continues, and you won't get an exception.
I'm not sure what exactly happened but I went ahead and reset my migrations once more and when I reseeded it worked. So if anyone else has this problem in the future, just clear the database again and try again.
In my case I had a syntax error when calling Rake :: .....
It worked for me to update the gem with gem 'faker', '~> 2.15', '> = 2.15.1'
Then rails db: migrate: reset and rails db: seed.

NoMethodError: undefined method `with_deleted`

I am trying to seed my database, but I am running into this error where its telling me with_deleted is an undefined method in my seeds.rb:
User.with_deleted.each(&:really_destroy!)
User.create(
email: 'admin#example.com',
roles: [:admin],
password: 'abc123',
password_confirmation: 'abc123',
)
I believe it should have been defined from the paranoia gem. Can anyone suggest a workaround? I am working in Rails 5.1.
I tried both of these:
#user = User.create(
email: 'admin#example.com',
roles: [:admin],
password: 'abc123',
password_confirmation: 'abc123',
)
and
User.create(
email: 'admin#example.com',
roles: [:admin],
password: 'abc123',
password_confirmation: 'abc123',
)
and it looked like it worked but when I did a User.last in rails c I still got nil
As far as including act_as_paranoid in my User.rb file, this is what I have:
# acts_as_paranoid
include AddressFields
include MeAssessmentScoring
Is this not enough? But ultimately, can I just get this working without having to involve the paranoia gem? So far, what I have tried above has not worked and when I try to create a user within rails c, I get:
ActiveRecord::RecordInvalid: Validation failed: First name can't be
blank, Last name can't be blank
when I ran this: user = User.create!(email:'admin1#example.com', roles:'admin',password:'abc123', password_confirmation:'abc123')
I was able to get up and running by executing the following:
heroku pg:backups:capture
heroku pg:backups:download
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U myuser -d thirdelementweb_development latest.dump

seeding db with users using devise fails: ActiveRecord::RecordInvalid: Validation failed: Email is invalid

I am trying to run a simple loop to create multiple seed users:
In seeds.rb:
5.times do
user = User.new(
email: RandomData.random_space_email,
password: 'teatea',
password_confirmation: 'teatea'
)
user.skip_confirmation!
user.save!
end
RandomData.random_space_email:
def self.random_space_email
"#{##names.sample}##{##space_words.sample}.#{##space_words.sample}"
end
When I run rake db:reset I get this error: ActiveRecord::RecordInvalid: Validation failed: Email is invalid
however if I set the loop to run only once (1.times do) everything works as expected.
Any help would be wonderful!
After carefully checking:
def self.random_space_email
"#{##names.sample}##{##space_words.sample}.#{##space_words.sample}"
end
I found that ##space_words sometimes contains strings with spaces... and this seems to be causing devise to throw an error, as there cannot be whitespace in an email address.

Rolify fixtures setup for rails app

Have an app where i'm using Rolify and Pundit for authorization and Minitest/fixtures for testing.
When i setup a user as administrator using fixtures, i'm not getting back a true for an assertion.
/fixtures/users.yml
administrator:
email: admin#example.com
roles: admin
/fixtures/roles.yml
admin:
id: 1
name: admin
test.rb
it "is administrator" do
User.first.has_role?('admin').must_equal true
end
The test comes back as false, i've tried the admin with a symbol and still get the same thing.
When a user in that development environment has admin, i get true when i ask if the role is admin.
Test framework is minitest and my user model is rolified
Alright so the problem was related to setting up roles.yml correctly. I was using id: above and if you just use users: it works
/fixtures/users.yml
administrator:
email: admin#example.com
roles: admin
/fixtures/roles.yml
admin:
users: administrator
name: admin
test.rb
it "is administrator" do
User.first.has_role?('admin').must_equal true
end

Application with only one user admin without sign up part

I did all Michael Hartl tutorial's application, and now I would like to create my on one, a writer assistant to edit a book.
I want to have only one user in database and it will be the admin. In the menu, only a signin button to connect and begin writting the book.
I'm wondering how to do and what is the best way to put only one user in the database. As in the tutorial there is the sign up part.
I hope you understand what I mean, and sorry for my bad english
Thanks
If you did want database manipulation/seeding
you could either
a) add a create statement in the seed.rb file
admin_user = User.create!(first_name: "test",
last_name: "test",
password: "123",
password_confirmation: "123",
roles: ["admin"])
then from the command line within your project run
rake db:seed
b) From the command line run
rails console
This gives you a command line loaded rails environment.
Where you can execute
admin_user = User.create!(first_name: "test",
last_name: "test",
password: "123",
password_confirmation: "123",
roles: ["admin"])

Resources