RSpec Timecop failures around Date appear to be timezone related - ruby-on-rails

We have some tests (specs) that are failing around Date/Time. Guessing it's a UTC issue but not sure why these specs passed last time project was touched (~8-months ago)!?!
#spec/features/comments/creation_spec.rb
feature 'Comment creation', type: :feature, js: true do
include CommentsPageHelpers
...
let!(:current_date) { Date.parse('2017-01-03') }
...
background do
Timecop.freeze(current_date)
...
end
after do
Timecop.return
end
shared_examples 'added comment' do |position:, text:|
scenario 'adds single comment' do
...
expect(page).to have_text 'January 3rd, 2017'
end
end
end
View (Angular template) #app/views/templates/comment.html.slim
.comment
...
{{ comment.createdAt | moment: 'MMMM Do, YYYY' }}
#RSpec Failure
Comment creation for image behaves like added comment adds single comment
Failure/Error: expect(page).to have_text 'January 3rd, 2017'
expected to find text "January 3rd, 2017" in "John Snow First comment message January 2nd, 2017Remove"
Shared Example Group: "added comment" called from ./spec/features/comments/creation_spec.rb:76
# ./spec/features/comments/creation_spec.rb:42:in `block (4 levels) in <top (required)>'
# ./spec/features/comments/creation_spec.rb:39:in `block (3 levels) in <top (required)>'

Changing the let! line to the two lines below seems to have fixed things. Don't know what else it might break or if it will break for devs in other timezones?!?
Time.zone = 'Pacific Time (US & Canada)'
let!(:current_date) { Time.parse('2017-01-03').in_time_zone }

Related

RSpec - A JSON text must at least contain two octets

Two failing specs after upgrading rspec-rails (2.5.2 -> 3.8.1) and capybara (2.18.0 -> 3.10.1):
Not really sure what's going on here. Looks like text in the expectation is being truncated?!?
let(:story_attributes) do
{
title: 'Edited title',
description: 'Edited location',
start_year: '2001',
start_month: 'December',
start_day: '5',
end_year: '2001',
end_month: 'October',
end_day: '10',
is_range: true,
cover_image: {
url: 'http://placehold.it/edited.png'
}
}
end
...
within 'section.story-cover' do
expect(page).to have_text 'Edited title'
expect(page).to have_text 'Edited location'
expect(page).to have_text 'December 5th - October 10th, 2001'
end
In first failed example (below) "Edited location" is being truncated.
In second example expect(page).to have_text 'Edited title Edited location' where only "Edited title\nEdited locat" is found.
Then there's this "JSON text must at least contain two octets" issue which may or may not be related but this used to pass before upgrading rspec-rails & capybara.ds
Thoughts?
RSpec Failures:
1) Story editing published edit story
Failure/Error: JSON.parse(response.body)
JSON::ParserError:
A JSON text must at least contain two octets!
# ./app/services/converter/image_service.rb:36:in `post_to_filepicker'
# ./app/services/converter/image_service.rb:18:in `convert_format'
# ./app/services/converter/image_service.rb:11:in `block in convert'
# ./app/services/converter/image_service.rb:10:in `each'
# ./app/services/converter/image_service.rb:10:in `convert'
# ./app/models/images/image.rb:5:in `convert'
# ./app/models/images/image.rb:20:in `enqueue_conversion'
# ./app/services/story/updating_service.rb:14:in `update'
# ./app/controllers/stories_controller.rb:58:in `update'
# ------------------
# --- Caused by: ---
# Capybara::ExpectationNotMet:
# expected to find text "Edited location" in "Edited title\nLyla HoegerEditedDecember 5th - October 10th, 2001Download"
# ./spec/features/stories/editing_spec.rb:86:in `block (4 levels) in <top (required)>'
2) Story editing private private story should be read after editing
Failure/Error: JSON.parse(response.body)
JSON::ParserError:
A JSON text must at least contain two octets!
# ./app/services/converter/image_service.rb:36:in `post_to_filepicker'
# ./app/services/converter/image_service.rb:18:in `convert_format'
# ./app/services/converter/image_service.rb:11:in `block in convert'
# ./app/services/converter/image_service.rb:10:in `each'
# ./app/services/converter/image_service.rb:10:in `convert'
# ./app/models/images/image.rb:5:in `convert'
# ./app/models/images/image.rb:20:in `enqueue_conversion'
# ./app/services/story/updating_service.rb:14:in `update'
# ./app/controllers/stories_controller.rb:58:in `update'
# ------------------
# --- Caused by: ---
# Capybara::ExpectationNotMet:
# expected to find text "Edited title Edited location" in "Edited title\nEdited locat"
# ./spec/features/stories/editing_spec.rb:115:in `block (4 levels) in <top (required)>'
One of the big changes between Capybara 2.x and 3.x was that in Capybara 3.x text is returned as closely to what is displayed as possible. This means that line feeds are now included in the returned text when they would display to the user - https://github.com/teamcapybara/capybara/blob/master/UPGRADING.md. You either need to change your expected text to "Edited title\nEdited location" at spec/features/stories/editing_spec.rb:115 or if you don't care about the linefeeds you can use the :normalize_ws option => expect(page).to have_text("Edited title Edited location", normalize_ws: true)

Poltergeits doesn't working well in rspec tests

I have the following feature tests passing:
require 'rails_helper'
describe "Create a quetsion", type: :feature do
let(:question) { build(:question) }
before do
login_as create(:user, :teacher)
exercise = create(:exercise)
visit new_exercise_question_url(exercise.id)
end
context "whit valid attributes" do
subject do
fill_in "question_score", with: question.score
fill_in "question_description", with: question.description
click_on "Criar"
end
it "create the question" do
expect{ subject }.to change(Question, :count).by(1)
expect(page).to have_current_path(question_path(Question.first.id))
end
end
context "whit invalid attributes" do
subject do
click_on "Criar"
end
it "doesn't create the exercise" do
expect{ subject }.to change(Question, :count).by(0)
expect(page).to have_selector("div.alert.alert-danger")
end
end
end
That works fine, unless I add js: true. In this case, I have the following errors:
1) Create a quetsion whit valid attributes create the question
Failure/Error: fill_in "question_score", with: question.score
Capybara::ElementNotFound:
Unable to find field "question_score"
# /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/node/finders.rb:44:in `block in find'
# /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/node/base.rb:85:in `synchronize'
# /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/node/finders.rb:33:in `find'
# /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/node/actions.rb:85:in `fill_in'
# /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/session.rb:735:in `block (2 levels) in <class:Session>'
# /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/dsl.rb:52:in `block (2 levels) in <module:DSL>'
# ./spec/features/create_a_question_spec.rb:14:in `block (3 levels) in <top (required)>'
# ./spec/features/create_a_question_spec.rb:20:in `block (4 levels) in <top (required)>'
# ./spec/features/create_a_question_spec.rb:20:in `block (3 levels) in <top (required)>'
2) Create a quetsion whit invalid attributes doesn't create the exercise
Got 0 failures and 2 other errors:
2.1) Failure/Error: visit new_exercise_question_url(exercise.id)
Capybara::Poltergeist::StatusFailError:
Request to 'http://www.example.com/exercises/1/questions/new' failed to reach server, check DNS and/or server status
# /usr/local/rvm/gems/ruby-2.3.1/gems/poltergeist-1.13.0/lib/capybara/poltergeist/browser.rb:376:in `command'
# /usr/local/rvm/gems/ruby-2.3.1/gems/poltergeist-1.13.0/lib/capybara/poltergeist/browser.rb:35:in `visit'
# /usr/local/rvm/gems/ruby-2.3.1/gems/poltergeist-1.13.0/lib/capybara/poltergeist/driver.rb:97:in `visit'
# /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/session.rb:240:in `visit'
# /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/dsl.rb:52:in `block (2 levels) in <module:DSL>'
# ./spec/features/create_a_question_spec.rb:8:in `block (2 levels) in <top (required)>'
2.2) Failure/Error: #socket.send(command.id, command.message, receive_timeout) or raise DeadClient.new(command.message)
Capybara::Poltergeist::DeadClient:
PhantomJS client died while processing {"id":"2928bf26-2dd8-45d7-8822-41b2923fd40d","name":"reset","args":[]}
# /usr/local/rvm/gems/ruby-2.3.1/gems/poltergeist-1.13.0/lib/capybara/poltergeist/server.rb:38:in `send'
# /usr/local/rvm/gems/ruby-2.3.1/gems/poltergeist-1.13.0/lib/capybara/poltergeist/browser.rb:369:in `command'
# /usr/local/rvm/gems/ruby-2.3.1/gems/poltergeist-1.13.0/lib/capybara/poltergeist/browser.rb:224:in `reset'
# /usr/local/rvm/gems/ruby-2.3.1/gems/poltergeist-1.13.0/lib/capybara/poltergeist/driver.rb:183:in `reset!'
# /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/session.rb:109:in `reset!'
# /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara.rb:334:in `block in reset_sessions!'
# /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara.rb:334:in `reverse_each'
# /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara.rb:334:in `reset_sessions!'
# /usr/local/rvm/gems/ruby-2.3.1/gems/capybara-2.10.1/lib/capybara/rspec.rb:21:in `block (2 levels) in <top (required)>'
I checked, whit js: true my page html isn't written too (instead the desired HTML, I only receive <html><head></head><body></body></html>).
I added poltergeist gem to test javascritp, and configured it in my rspec rails_helper.rb file:
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
PhantomJS is already instaled and available in my $PATH. I'm completelly out of ideas, what can be happening here?
There are a number of potential issues here.
If you have set Capybara.server = :puma, make sure you're not running puma 3.7.0 which has a bug in it that will be fixed when 3.7.1 is released. For now use 3.6.9
If this is the first time you're setting up js tests make sure you have setup and correctly configured DatabaseCleaner so that you are no running in transaction mode for js: true tests. See https://github.com/teamcapybara/capybara#transactions-and-database-setup and https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example for the recommended configuration
Once you have #1 and #2 worked out then you'll need to look at your tests and deal with the fact that after a click/click_on type action you need to check for a visible page change to ensure the action has completed before you move on. This is because clicks happen but the actions those clicks trigger can occur asynchronously. In your current example that means you would need something like
...
click_on "Criar"
expect(page).to have_text("Question created!") #whatever message is shown when the action has completed, or check the page etc.
Without something like that the change check will fail because it will check the count before the actual actually occurs.
Default to _path helpers rather than _url helpers when there's no need for a specific host name (99.9% of the time). This lets Capybara fill_in the correct host/port for the server being run. To visit _url helpers there are a number of configuration parameters that need to be carefully set.
Note: It's generally not best practice to be checking database counts in feature tests, they should generally be limited to checking on visible page changes.

Routes Increment when I Run Rspec feature

So, for some reason my routes increment by 11 when I run my comment feature test.
Exhibit A: I hit save on my comment_spec.rb
RSpec.feature "Adding comments to movies" do
before do
#kyle = Admin.create(email: "kyle#example.com", password: "password")
#jill = Member.create(email: "jill#example.com", password: "password")
#movie = Movie.create!(title: "First movie", synopsis: "Synopsis of first movie", year_released: '2000', admin: #kyle)
end
scenario "permits a signed in member to write a comment" do
login_as(#jill, scope: :member)
visit "/"
click_link #movie.title
fill_in "New Review", with: "An awesome movie"
click_button "Add Review"
expect(page).to have_content("Review has been created")
expect(page).to have_content("An awesome movie")
# must implement a nested route in order for this to work.
expect(current_path).to eq(movie_path(#movie.comments.last.id))
end
end
Then the test passes
moviesmoviesmovies/spec/features/comments_spec.rb:3)
Finished in 0.99879 seconds (files took 2.4 seconds to load) 1
example, 0 failures
Now, I hit enter in the terminal and get this:
Adding comments to movies permits a signed in member to write a comment
Failure/Error: expect(current_path).to eq(movie_path(#movie.comments.last.id))
expected: "/movies/2"
got: "/movies/4"
(compared using ==)
# ./spec/features/comments_spec.rb:26:in `block (2 levels) in <top (required)>'
Then I save the comment_spec.rb again and get this:
Adding comments to movies permits a signed in member to write a comment
Failure/Error: expect(current_path).to eq(movie_path(#movie.comments.last.id))
expected: "/movies/3"
got: "/movies/15"
(compared using ==)
# ./spec/features/comments_spec.rb:26:in `block (2 levels) in <top (required)>'
After this happens, I run:
bundle exec rails db:test:prepare
Then the tests pass again, but the above repeats. What in the world is making this happen? haha, but really?
My GitHub if needed.
The issue you're having is that your database isn't being reset after each feature test, as you likely expect it is. Specifically, the problem is with Capybara and the config.use_transactional_fixtures = true line which is set in your spec config.
Capybara effectively tests your application as an external process using a virtual browser. This means that your capybara tests only have visibility into the client-side behavior of your app (i.e. the page variable). In other words, from Capybara's perspective your app is a "Black box". Therefore, capaybara doesn't see controller objects like your session, params, or request variables, nor can it see or control the specific database transactions which occur during testing.
Rather than relying on transactional fixtures, consider using database cleaner, which manually resets the database after each feature test. You can find the gem here: https://github.com/DatabaseCleaner/database_cleaner. Make sure you follow the directions on integrating with Capybara: https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example.

CSV file appearing backwards and RSpec failing

I am new to Ruby in general and cannot find a solution as to why my entries_2.csv is imported backwards. My other .csv import appears to be the same syntax and is working fine.
Here is the portion of my RSpec for .csv method:
require_relative "../models/address_book"
RSpec.describe AddressBook do
let(:book) {AddressBook.new}
def check_entry(entry, expected_name, expected_phone_number, expected_email)
expect(entry.name).to eq(expected_name)
expect(entry.phone_number).to eq(expected_phone_number)
expect(entry.email).to eq(expected_email)
end
describe "#import from csv" do
it "imports the correct number of entries" do
book.import_from_csv("entries.csv")
book_size = book.entries.size
expect(book_size).to eq 5
end
it "imports the 1rst entry" do
book.import_from_csv("entries.csv")
entry_one = book.entries[0]
check_entry(entry_one, "Bill", "555-555-4854", "bill#blocmail.com")
end
it "imports the 2nd entry" do
book.import_from_csv("entries.csv")
entry_two = book.entries[1]
check_entry(entry_two, "Bob", "555-555-5415", "bob#blocmail.com")
end
it "imports the 3rd entry" do
book.import_from_csv("entries.csv")
entry_three = book.entries[2]
check_entry(entry_three, "Joe", "555-555-3660", "joe#blocmail.com")
end
it "imports the 4th entry" do
book.import_from_csv("entries.csv")
entry_four = book.entries[3]
check_entry(entry_four, "Sally", "555-555-4646", "sally#blocmail.com")
end
it "imports the 5th entry" do
book.import_from_csv("entries.csv")
entry_five = book.entries[4]
check_entry(entry_five, "Sussie", "555-555-2036", "sussie#blocmail.com")
end
it "imports an additional 3 entries" do
book.import_from_csv("entries_2.csv")
book_size = book.entries.size
expect(book_size).to eq 3
end
it "imports 1rst entry into additional csv" do
book.import_from_csv("entries_2.csv")
entry_one = book.entries[0]
check_entry(entry_one, "Ralph", "222-222-2222", "ralph#comcast.net")
end
it "imports 2nd entry into additional csv" do
book.import_from_csv("entries_2.csv")
entry_two = book.entries[1]
check_entry(entry_two, "Will", "333-333-3333","Will#comcast.net")
end
it "imports 3rd entry into additional csv" do
book.import_from_csv("entries_2.csv")
entry_three = book.entries[2]
check_entry(entry_three, "Foobarr", "444-444-4444", "Foobarr#comcast.net")
end
end
Here is my two .csv files:
entries.csv
name,phone_number,email
Bill,555-555-4854,bill#blocmail.com
Bob,555-555-5415,bob#blocmail.com
Joe,555-555-3660,joe#blocmail.com
Sally,555-555-4646,sally#blocmail.com
Sussie,555-555-2036,sussie#blocmail.com
entries_2.csv
name,phone_number,email
Ralph,222-222-2222,ralph#comcast.net
Will,333-333-3333,Will#comcast.net
Foobarr,444-444-4444,Foobarr#comcast.net
Here is the error I am receiving when checking my RSpec:
Failures:
1) AddressBook#import from csv imports 1rst entry into additional csv
Failure/Error: expect(entry.name).to eq(expected_name)
expected: "Ralph"
got: "Foobarr"
(compared using ==)
# ./spec/address_book_spec.rb:8:in `check_entry'
# ./spec/address_book_spec.rb:142:in `block (3 levels) in <top (required)>'
2) AddressBook#import from csv imports 2nd entry into additional csv
Failure/Error: expect(entry.name).to eq(expected_name)
expected: "Will"
got: "Ralph"
(compared using ==)
# ./spec/address_book_spec.rb:8:in `check_entry'
# ./spec/address_book_spec.rb:150:in `block (3 levels) in <top (required)>'
3) AddressBook#import from csv imports 3rd entry into additional csv
Failure/Error: expect(entry.name).to eq(expected_name)
expected: "Foobarr"
got: "Will"
(compared using ==)
# ./spec/address_book_spec.rb:8:in `check_entry'
# ./spec/address_book_spec.rb:158:in `block (3 levels) in <top (required)>'
Finished in 0.05492 seconds (files took 0.2885 seconds to load)
17 examples, 3 failures
Why is my entries.csv RSpec correct, but my entries_2.csv is not? What am I missing.
Thanks
Assuming AddressBook is an ActiveRecord model with has_many :entries defined on it, the core issue you're running into is that relations are not guaranteed to be returned in any specific order, unless you explicitly request it. So this:
book.entries[0]
Is actually running:
SELECT * FROM entries WHERE address_book_id = ?
And then fetching the first element of that array. Since the above query is not ordered, the order you get back is arbitrary (as you're seeing; sometimes it reflects creation order, and sometimes it does not).
What you actually want is something which actually requests the records ordered in the sequence they were created in, like:
book.entries.order('entries.id ASC')[0]
Which you can also accomplish by adding order: 'id ASC' as a parameter to your has_many :entries definition.
I finally figured out the solution to my problem. CSV.parse sorts .csv files alphabetically, I did not know this. At this point I am unsure how I could manipulate that particular method, however, rearranging my names in my .csv file solved my problem.

Understanding RSpec Failure

I'm getting these failures when running rspec spec/. The spec that is failing is was auto-generated with the scaffolding. I'm trying to understand RSpec but I don't know where to begin looking for the cause other than it feels like some method is missing?!? Yet, app appears to be working fine. Nothing about these failures appears in test.log. Is there another place I should be looking for hints to track this down?
$ rspec spec/
.....................F.F.
Failures:
1) clowns/edit.html.haml renders the edit clown form
Failure/Error: render
undefined method `to_sym' for nil:NilClass
# ./app/views/clowns/_form.html.haml:4:in `block in _app_views_clowns__form_html_haml__3590088286240866241_2176114460_3896491916910336970'
# ./app/views/clowns/_form.html.haml:1:in `_app_views_clowns__form_html_haml__3590088286240866241_2176114460_3896491916910336970'
# ./app/views/clowns/edit.html.haml:3:in `_app_views_clowns_edit_html_haml___574620942879655923_2176081980_599706797287605391'
# ./spec/views/clowns/edit.html.haml_spec.rb:13:in `block (2 levels) in <top (required)>'
2) clowns/new.html.haml renders new clown form
Failure/Error: render
undefined method `to_sym' for nil:NilClass
# ./app/views/clowns/_form.html.haml:4:in `block in _app_views_clowns__form_html_haml__3590088286240866241_2176114460_3896491916910336970'
# ./app/views/clowns/_form.html.haml:1:in `_app_views_clowns__form_html_haml__3590088286240866241_2176114460_3896491916910336970'
# ./app/views/clowns/new.html.haml:3:in `_app_views_clowns_new_html_haml__1085372210838170129_2159651900_599706797287605391'
# ./spec/views/clowns/new.html.haml_spec.rb:12:in `block (2 levels) in <top (required)>'
Finished in 1.03 seconds
27 examples, 2 failures, 2 pending
And here is the spec that apparently fails (edit.html.haml_spec.rb). It was auto-generated by rails g scaffold Clown name:string balloons:integer:
#spec/views/clowns/edit.html.haml_spec.rb
require 'spec_helper'
describe "clowns/edit.html.haml" do
before(:each) do
#clown = assign(:clown, stub_model(Clown,
:name => "MyString",
:balloons => 1
))
end
it "renders the edit clown form" do
render
# Run the generator again with the --webrat-matchers flag if you want to use webrat matchers
assert_select "form", :action => clown_path(#clown), :method => "post" do
assert_select "input#clown_name", :name => "clown[name]"
assert_select "input#clown_balloons", :name => "clown[balloons]"
end
end
end
Are you also using simple_form (or perhaps even formtastic)? I get the same error when I convert the scaffold forms from standards forms to simple_form forms (form_for(#user) => simple_form_for(#user)).
It still works, as you say, so I'm not sure it's worth worrying about. I think you'd be better off just letting Cucumber worry about it. Sarah Mei says view specs are a waste of time. :-)

Resources