An error occurred while loading ./spec/controllers/users_controller_spec.rb - ruby

I am creating factories to replace my fixtures and to generate Test Data. I've installed FactoryBot but I am getting an error when running "rspec". This is the outcome log I get:
An error occurred while loading ./spec/controllers/users_controller_spec.rb.
Failure/Error: #user = FactoryBot.create(:user)
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken
# ./spec/controllers/users_controller_spec.rb:5:in `block in <top (required)>'
# ./spec/controllers/users_controller_spec.rb:3:in `<top (required)>'
Finished in 0.00041 seconds (files took 38.38 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples
This is my spec/factories/user_factory.rb
FactoryBot.define do
factory :user do
email "peter#example.com"
password "0123456"
first_name "Peter"
last_name "Example"
admin false
end
end
This is my spec/controllers/users_controller_spec.rb
require 'rails_helper'
describe UsersController, type: :controller do
#user = FactoryBot.create(:user)
# let (:user) { User.create!(email:"achochaocierva#gmail.com", password: "Ratadecierva2")}
describe 'GET #show' do
context 'when a user is logged in' do
before do
sign_in user
end
it 'loads correct user details' do
get :show, params: { id: user.id }
expect(response).to be_ok
expect(assigns(:user)).to eq user
end
end
context 'when a user is not logged in' do
it 'redirects to login' do
get :show, params: { id: user.id }
expect(response).to redirect_to(new_user_session_path)
end
end
end
end
What would be the errors meaning? What am I doing wrong?

What would be the errors meaning? What am I doing wrong?
Your factory tries to create a user with the same email as one of the existing users (no wonder, seeing that the email is hardcoded in the factory). Your DB's uniqueness constraint complains.
You should use sequences for user emails
# email "peter#example.com"
sequence(:email) { |n| "factory_#{n}#example.com" }
First user from this factory will have email "factory_1#example.com", second - "factory_2#example.com", and so on.

Related

When Running Rspec and Sinatra, I keep getting ArgumentError: wrong number of arguments (given 2, expected 0)

I've got a class method called authenticate, which works on the User class.
def self.authenticate(email:, password:)
result = DatabaseConnection.query("SELECT * FROM users WHERE email = '#{email}'")
User.new(result[0]['id'], result[0]['email'])
end
I have an Rspec test;
feature 'authentication' do
it 'a user can sign in' do
User.create(email: 'test#example.com', password: 'password123')
visit 'sessions/new'
fill_in(:email, with: 'test#example.com')
fill_in(:password, with: 'password123')
click_button 'Sign In'
expect(page).to have_content 'Welcome, test#example.com'
end
end
When running Rspec, I get the following error;
1) authentication a user can sign in
Failure/Error:
def initialize(id:, email:)
#id = id
#email = email
end
ArgumentError:
wrong number of arguments (given 2, expected 0)
# ./lib/user.rb:15:in `initialize'
# ./lib/user.rb:23:in `new'
# ./lib/user.rb:23:in `authenticate'
# ./app.rb:84:in `block in <class:BookmarkManager>'
Below is my Sinatra app;
require 'sinatra/base'
require './lib/bookmark'
require './lib/user'
require './database_connection_setup.rb'
require 'uri'
require 'sinatra/flash'
require_relative './lib/tag'
require_relative './lib/bookmark_tag'
class BookmarkManager < Sinatra::Base
enable :sessions, :method_override
register Sinatra::Flash
get '/' do
"Bookmark Manager"
end
get '/bookmarks' do
#user = User.find(session[:user_id])
#bookmarks = Bookmark.all
erb :'bookmarks/index'
end
post '/bookmarks' do
flash[:notice] = "You must submit a valid URL" unless Bookmark.create(url: params[:url], title: params[:title])
redirect '/bookmarks'
end
get '/bookmarks/new' do
erb :'bookmarks/new'
end
delete '/bookmarks/:id' do
Bookmark.delete(id: params[:id])
redirect '/bookmarks'
end
patch '/bookmarks/:id' do
Bookmark.update(id: params[:id], title: params[:title], url: params[:url])
redirect('/bookmarks')
end
get '/bookmarks/:id/edit' do
#bookmark = Bookmark.find(id: params[:id])
erb :'bookmarks/edit'
end
get '/bookmarks/:id/comments/new' do
#bookmark_id = params[:id]
erb :'comments/new'
end
post '/bookmarks/:id/comments' do
Comment.create(text: params[:comment], bookmark_id: params[:id])
redirect '/bookmarks'
end
get '/bookmarks/:id/tags/new' do
#bookmark_id = params[:id]
erb :'/tags/new'
end
post '/bookmarks:id/tags' do
tag = Tag.create(content: params[:tag])
BookmarkTag.create(bookmark_id: params[:id], tag_id: tag.id)
redirect '/bookmarks'
end
get '/users/new' do
erb :'users/new'
end
post '/users' do
user = User.create(email: params[:email], password: params[:password])
session[:user_id] = user.id
redirect '/bookmarks'
end
get '/sessions/new' do
erb :'sessions/new'
end
post '/sessions' do
user = User.authenticate(email: params[:email], password: params[:password])
if user
session[:user_id] = user.id
redirect('/bookmarks')
else
flash[:notice] = 'Please check your email or password.'
redirect('/sessions/new')
end
end
run! if app_file == $0
end
Below is the full User class
require_relative './database_connection'
require 'bcrypt'
class User
def self.create(email:, password:)
encypted_password = BCrypt::Password.create(password
)
result = DatabaseConnection.query("INSERT INTO users (email, password) VALUES('#{email}', '#{encypted_password}') RETURNING id, email;")
User.new(id: result[0]['id'], email: result[0]['email'])
end
attr_reader :id, :email
def initialize(id:, email:)
#id = id
#email = email
end
def self.authenticate(email:, password:)
result = DatabaseConnection.query("SELECT * FROM users WHERE email = '#{email}'")
User.new(result[0]['id'], result[0]['email'])
end
def self.find(id)
return nil unless id
result = DatabaseConnection.query("SELECT * FROM users WHERE id = #{id}")
User.new(
id: result[0]['id'],
email: result[0]['email'])
end
end
What I don't understand is, why is Rspec saying it was expecting 0 arguments, when the initialize method clearly requires two arguments (id, and, email)?
I need to take the id and email method from authenticate and deliver it to initialize.
I thought that's what I was doing, but both Rspec and sinatra are saying otherwise.
Thanks, in advance.
Here you are passing id as sequential args (in the authenticate method).
User.new(result[0]['id'], result[0]['email'])
However your User.new expects keyword args:
def initialize(id:, email:)
Simply pass them this way:
User.new(id: result[0]['id'], email: result[0]['email'])
Also, just something I noticed, if your DatabaseConnection.query returns no results your authenticate will raise an error from result[0]['id'] (it will say "Undefined method [] for Nil:NilClass". Maybe you should fix this and add a test case for it, for example:
def self.authenticate(email:, password:)
result = DatabaseConnection.query(
"SELECT * FROM users WHERE email = '#{email}'"
)
record = result[0]
if record
User.new(id: result[0]['id'], email: result[0]['email'])
end
end
This way the method will return nil if there's no matching user, and your if user inside post '/sessions' will work properly.

How to hook after example has execution result and status with :aggregated_failures flag

I'm maintaining a standalone test automation suite written in Rspec & Capybara and SitePrism (No Rails).
Recently I started integrating it with Testrail for reporting - I used rspec-testrail gem for that but I had to modify it a bit, because I also wanted to send Slack notifications with test progress and report.
Anyway, the integration works smoothly, except the fact that example processing is relying on example having an exception and lack of exception causes setting the test status as Passed on Testrail.
As it appears, after :each nor after :example in Rspec.configure doesn't guarantee that the example has finished running.
I also tried around :example and around :each as described here, but to no avail.
I inspected contents of example.metadata and it looks that example.metadata[:execution_result] has only started_at variable, but a finished example would have also finished_at and status variables, according to the docs
My suspicion (after reading the relish docs) is that :aggregated_failures is the cause of different metadata structure and multiple expects running in threads that are later merged into one backtrace.
Do you know how can I wait for the example to finish or how to hook into the state where it's finished?
Or maybe I should create a custom formatter where I would hook after example notifications printed to the console (I would like to keep the stacktrace there).
My code is as follows:
Test (both assertions are failing):
require 'spec_helper'
feature 'Sign in' do
let(:login_page) { LoginPage.new }
let(:user) { { email: ENV['ADMIN_EMAIL'], password: ENV['ADMIN_PASSWORD'] } }
scenario 'is successful and user is redirected to dashboard for user with correct credentials', testrail_id: 5694 do
login_page.load
login_page.form.email.set(user[:email])
login_page.form.password.set(user[:password])
login_page.form.submit_btn.click
expect(login_page.sidebar).to have_jobs(text: "some_nonexistenttext")
login_page.logout
expect(current_url).to have_content "google.com"
end
end
Console output from the above test:
Failures:
1) Sign in is successful and user is redirected to dashboard for user with correct credentials
Got 2 failures:
1.1) Failure/Error: expect(login_page.sidebar).to have_jobs(text: "blala")
expected #has_jobs?({:text=>"some_nonexistenttext"}) to return true, got false
# ./spec/auth/login_spec.rb:13:in `block (3 levels) in <top (required)>'
1.2) Failure/Error: expect(current_url).to have_content "google.com"
expected to find text "google.com" in "https://example.com/"
# ./spec/auth/login_spec.rb:15:in `block (3 levels) in <top (required)>'
Finished in 53.91 seconds (files took 1.45 seconds to load)
4 examples, 1 failure
Failed examples:
rspec ./spec/auth/login_spec.rb:8 # Sign in is successful and user is redirected to dashboard for user with correct credentials
Spec helper:
require 'rubygems'
require 'capybara/rspec'
require 'selenium-webdriver'
require 'site_prism'
require 'slack-notifier'
require_relative '../config'
require_relative '../lib/testrail'
...
RSpec.configure do |config|
config.define_derived_metadata do |meta|
meta[:aggregate_failures] = true
end
config.example_status_persistence_file_path = 'examples.txt'
config.before :all do
testrail_initialize_test_run!
end
config.after :example, testrail_id: proc { |value| !value.nil? } do |example|
RSpec::Testrail.process(example)
end
end
processing method (slightly modified from original)
def process(example)
if example.exception
status = 5
message = example.exception.message
slack_notifier.publish(message_text "Failed")
elsif example.skipped? || example.pending?
puts 'Example skipped or pending'
status = 10
message = 'Pending or not implemented'
else
status = 1
message = ''
end
client.send_post("add_result_for_case/#{testrun['id']}/#{example.metadata[:testrail_id]}",
status_id: status,
comment: message)
end
So basically all I had to was to use a reporter listener and process notifications inside it :)
config.reporter.register_listener RSpec::Testrail::Listener.new, :start, :example_failed, :example_passed, :example_pending, :stop

Tests fail with record creation in Rails 5

I am building an app in school and I am running into this error. As of right now the app walk through was started in rails 4.2.6, and I am running 5.0.0.1.
The error is:
Failures:
1) Post Creation can be created
Failure/Error: expect(#post).to be_valid
expected #<Post id: nil, date: "2016-12-20", rationale: "Anything", created_at: nil, updated_at: nil, user_id: nil> to be valid, but got errors: User must exist
# ./spec/models/post_spec.rb:10:in `block (3 levels) in <top (required)>'
Finished in 0.65569 seconds (files took 2.19 seconds to load)
10 examples, 1 failure
Failed examples:
rspec ./spec/models/post_spec.rb:9 # Post Creation can be created
My code is as follows. I have compared to the repo on the walk-through and it matches perfectly. What am i missing?
require 'rails_helper'
RSpec.describe Post, type: :model do
describe "Creation" do
before do
#post = Post.create(date: Date.today, rationale: "Anything")
end
it "can be created" do
expect(#post).to be_valid
end
it "cannot be created without a date and rationale" do
#post.date = nil
#post.rationale = nil
expect(#post).to_not be_valid
end
end
end
Rails 5 differs from Rails 4 in that when you have a belongs_to relation, Rails 5 will automatically validate the presence of the associated object, even without you adding any validations.
Probably your Post model belongs to a User. Because of this, you need to create a user in your test setup, or the validation will fail:
describe "Creation" do
before do
#user = User.create( ... )
#post = Post.create(date: Date.today, rationale: "Anything", user: #user)
end
it "can be created" do
expect(#post).to be_valid
end
it "cannot be created without a date and rationale" do
#post.date = nil
#post.rationale = nil
expect(#post).to_not be_valid
end
end

Rspec testing if text is pluralized (Hartl 10.5 Exercise 1)

Ruby 1.9.3, Rails 3.2.2
I'm trying to write an Rpsec (Capybara) to test if my page is properly pluralizing the word "Post". I get an error when running my Rspec that states:
c:/.../my_app/spec/requests/user_pages_spec.rb:58: in `block (3 levels) in ': undefined local variable or method 'user' for ... (NameError)
Here's the relevant test:
describe "profile page" do
let(:user){FactoryGirl.create(:user)}
let!(:m1){FactoryGirl.create(:micropost, user: user, content: "Food")}
let!(:m2){FactoryGirl.create(:micropost, user: user, content: "Bar")}
before {visit user_path(user)}
it {should have_selector('h1', text: user.name)}
it {should have_selector('title', text: user.name)}
describe "pagination" do
before(:all){40.times {FactoryGirl.create(:micropost, user: user, content: "Food")}}
after(:all){User.delete_all}
it {should have_selector('div.pagination')}
end
describe "microposts" do
it {should have_content(m1.content)}
it {should have_content(m2.content)}
it {should have_content(user.microposts.count)}
before do
sign_in user
visit root_path
User.delete_all
end
it "should pluralize post numbers" do
FactoryGirl.create(:micropost, user: user, content: "Food")
page.should have_content("1 micropost")
2.times {FactoryGirl.create(:micropost, user: user, content: "Food")}
page.should have_content("2 microposts")
end
I'm not sure if I'm going about testing for pluralization of posts the right way either, but I'm mainly stumped about why that block can't "see" the users object since the line right after my if statement can see it. if I comment out the if block everything runs fine.
Any code outside of it doesn't have access to the variables defined with before, let, etc. You need to place it inside an it call, say with a text argument that describes the total if/else test. Could you update this question to include the other code from your spec that's in scope?
Profile page doesn't have pluralization to test.
The test should be for static_pages#home.
describe 'Home page' do
describe 'for signed-in users' do
let(:user) { FactoryGirl.create(:user) }
before do
FactoryGirl.create(:micropost, user: user, content: 'Lorem ipsum')
FactoryGirl.create(:micropost, user: user, content: 'Dolor sit amet')
sign_in user
visit root_path
end
it { should have_content('micropost'.pluralize(user.microposts.count)) }
end
end

rails rspec mock_model Object expected, got String

I have a Directory controller and a Files controller. I'm testing the Files controller. I've created valid attributes for the File and I'm trying to mock_model the Directory to get the test to pass. The GET tests all work, but none of the POST tests work. The POST tests all give the error: "Directory expected, got String."
describe FilesController do
def valid_attributes {
:name => "test",
:reference_id => 1,
:location => "/path/to/directory",
:software => "excel",
:software_version => "2010",
:directory => mock_model(Directory)
}
end
describe "POST create" do
describe "with valid params" do
it "creates a new AssemblyFile" do
expect {
post :create, :assembly_file => valid_attributes
}.to change(AssemblyFile, :count).by(1)
end
it "assigns a newly created assembly_file as #assembly_file" do
post :create, :assembly_file => valid_attributes
assigns(:assembly_file).should be_a(AssemblyFile)
assigns(:assembly_file).should be_persisted
end
it "redirects to the created assembly_file" do
post :create, :assembly_file => valid_attributes
response.should redirect_to(AssemblyFile.last)
end
end
end
1) FilesController POST create with valid params creates a new File
Failure/Error: post :create, :file => valid_attributes
ActiveRecord::AssociationTypeMismatch:
Directory(#87017560) expected, got String(#49965220)
# ./app/controllers/files_controller.rb:60:in `new'
# ./app/controllers/files_controller.rb:60:in `create'
# ./spec/controllers/files_controller_spec.rb:79:in `block (5 levels) in <top (required)>'
# ./spec/controllers/files_controller_spec.rb:78:in `block (4 levels) in <top (required)>'
If I look at the test.log file, it shows that assembly is a string ("assembly" => "1011"). So I'm not sure why the mock_model isn't creating an object?
I've tried using stub! instead of mock_model, but that gets complicated because the create! used for stub! needs a lot of its own valid variables set and I don't really want to have to set a whole bunch of other valid attributes for that when I'm not trying to even test the Directory controller at all.
What am I doing wrong in my approach here?
Pass the id of the mock in the params hash instead of the mock itself. You'll also need to stub the find method so the mock is available in the controller action:
#directory = mock_model(Directory)
Directory.stub(:find).with(#directory.id).and_return(#directory)
post :create, :assembly_file => valid_attributes.merge(:directory_id => #directory.id)
# in controller
#directory = Directory.find(params[:assembly_file][:directory_id]) # => returns the mock

Resources