Cucumber rails test result - ruby-on-rails

I am learning cucumber / capybara on rails. I working on a scenario that expects to display sorted data from a table. I cannot figure out this message:
pending # express the regexp above with the code you wish you had
I have this table:
| title | rating | release_date |
| Aladdin | G | 25-Nov-1992 |
| The Terminator | R | 26-Oct-1984 |
| When Harry Met Sally | R | 21-Jul-1989 |
| The Help | PG-13 | 10-Aug-2011 |
| Chocolat | PG-13 | 5-Jan-2001 |
| Amelie | R | 25-Apr-2001 |
| 2001: A Space Odyssey | G | 6-Apr-1968 |
| The Incredibles | PG | 5-Nov-2004 |
| Raiders of the Lost Ark | PG | 12-Jun-1981 |
| Chicken Run | G | 21-Jun-2000 |
Below is my step definition:
When(/^I clicked "(.*?)"$/) do |arg1|
if arg1 == "Movie Title"
click_link ("title_header")
end
end
Then(/^I expect "(.*)" before "(.*)"$/) do |arg1, arg2|
assert page.body.expect =~ /arg1.*arg2/
end
The message again I am trying to resolve is below:
You can implement step definitions for undefined steps with these snippets:
Then(/^I expect to see "(.*?)" before "(.*?)"$/) do |arg1, arg2|
pending # express the regexp above with the code you wish you had
end
Thank you for the help.

You need to define the step correctly, you are missing the 'to see' in your step definition

Related

Rails 5: Does ActiveRecord uses id sequence when using build

I have 2 models which have 1 to 1 relationship, namely Project and Inspection, the structure is something like,
class Project < ApplicationRecord
has_one :inspection
def inspection_required?
(inspection || build_inspection).required?
end
end
class Inspection < ApplicationRecord
belongs_to :project
def required?
required_saved || API.check({ id: id})
end
end
As you see above, for a Project if we don't have an Inspection it does build_inspection. I looked at the database and it seems like the inspection record id skips the sequence as shown below,
ID | PROJECT_ID | VENDOR_TYPE | REQUIRED_SAVED | CREATED_AT | UPDATED_AT |
---|--------------|-------------|----------------|---------------------|----------------------|
1 | 7983f1a33d55 | vendor1 | false | 2020-09-12 00:43:52 | 2020-09-12 00:43:52 |
3 | asdereww224f | vendor2 | true | 2020-09-12 00:48:51 | 2020-09-12 00:48:51 |
4 | 3q3332reere1 | vendor1 | false | 2020-09-13 01:02:53 | 2020-09-13 01:02:53 |
7 | 1df343qe233a | vendor2 | true | 2020-09-14 02:43:51 | 2020-09-14 02:43:51 |
The ID column is skipping sequence.
So my question is, does build_inpsection is using the Sequence generated by Postgres SQL even when we don't save the record?
Rails version: Rails 5.2.3
Postgres version: 9.6

Why does my Cucumber scenario fail with "Invalid keys :text, should be one of :text, ..."?

I have the following code to verify that a form only accepts date values in dd-mm-yyyy format. However, I get the error
Invalid keys :text, should be one of :text, :visible, :between, :count, :maximum, :minimum, :exact, :match, :wait
I presume this is caused by fill_in arg2 in my step sefinition, but I can't figure out what is actually wrong.
Feature file:
Scenario Outline: Edit Person Validation
Given I login
And I edit a person
When I enter <value> as <field>
Then I should see <validity>
Scenarios: valid
| value | field | validity |
| 20-05-2014 | person_startdate | success |
| JA-61-47-66-C | person_nino | success |
Scenarios: invalid
| value | field | validity |
| 05-20-2014 | person_startdate | failure |
| 2014-05-20 | person_startdate | failure |
| 2014-20-05 | person_startdate | failure |
| 20/05/2014 | person_startdate | failure |
| 20-05/2014 | person_startdate | failure |
| 20/05-2014 | person_startdate | failure |
| 20/05-2014 | person_startdate | failure |
| 20'05'2014 | person_startdate | failure |
| Today | person_startdate | failure |
| Tomorrow | person_startdate | failure |
| 2014 | person_startdate | failure |
| March | person_startdate | failure |
| 05-20 | person_startdate | failure |
| JH-22-43-61 | person_nino | failure |
Step definition:
Given /^I login$/ do
login "*****", "*****"
end
Given /^I edit a person$/ do
visit edit_person_path('1682')
end
When /^I enter (.+) as (.+)$/ do |arg1,arg2|
fill_in arg2, with: arg1
click_on 'Save'
end
Then /^I should see failure$/ do
expect(page).to have_text('div', text: 'prohibited')
end
Then /^I should see success$/ do
expect(page).to have_selector('h1', text: 'Your People')
end
def login(username, password)
visit login_path
#user = User.create(username: username, password: password)
fill_in "username_or_email", with: #user.username
fill_in "login_password", with: #user.password
click_on "Log In"
end
I'm guessing that the error is in
Then /^I should see failure$/ do
expect(page).to have_text('div', text: 'prohibited')
end
That's not how have_text is used. Do either
expect(page).to have_selector('div', text: 'prohibited')
or (only if you don't care about checking that the text is in a div, which maybe you don't since that's not a very specific selector)
expect(page).to have_text('prohibited')

Why isn't Rspec reloading the contents of my database table?

An integration spec includes:
click_on "submit_order"
order = Order.last
binding.pry_remote
Why, then, is the following possible?
And how do I fix it?
From: /Users/steven/Dropbox/Testivate/spec/features/order_spec.rb # line 44 :
43: order = Order.last
=> 44: binding.pry_remote
[1] pry(#<RSpec::Core::ExampleGroup::Nested_1::Nested_1>)> order
=> nil
[2] pry(#<RSpec::Core::ExampleGroup::Nested_1::Nested_1>)> Order.last
+----+----------+----------+----------+----------+----------+---------+----------+----------+----------+----------+
| id | company | url | comp1 | comp2 | comp3 | user_id | guest... | guest... | creat... | updat... |
+----+----------+----------+----------+----------+----------+---------+----------+----------+----------+----------+
| 1 | The Nile | http:... | http:... | http:... | http:... | 1 | | | 2014-... | 2014-... |
+----+----------+----------+----------+----------+----------+---------+----------+----------+----------+----------+
1 row in set
UPDATE:
I tried:
Order.connection.reconnect!
No change.
UPDATE II
When you change the integration spec to read...
click_on "submit_order"
save_and_open_page
order = Order.last
...the Order table reloads correctly and order is the last Order.
But why?
There are no callbacks on Order or User.
The HTTP POST operation is not synchronous with the database update, so your Order.last method call is likely getting completed before the database write has been completed. You would "fix" this by awaiting some positive confirmation that the submission has been completed (e.g. redirect, flash message, etc.).

Rails: update mutiple attribute of multiple model concurrently

I want to update values taken from a single form in two different model at the same time, I have written the following code to do so :-
if #mess.update_attributes!(:mess_name => params[:mess_name_update], :mess_capacity => params[:mess_capacity_update]) && #mess_price.update_attributes!(:breakfast_charge => params[:mess_breakfast_charge_update], :lunch_charge => params[:mess_lunch_charge_update], :dinner_charge => params[:mess_dinner_charge_update], :monthly_charge => params[:mess_monthly_charge_update], :semesterly_charge => params[:mess_semesterly_charge_update], :start_date => params[:start_date_update], :end_date => params[:end_date_update], :rebate => params[:rebate_update])
flash[:success] = "Mess Details Updated Successfully!!"
else
flash[:error] = "Some Error! Please Try Again!"
end
But the above code is giving following error
ActiveRecord::RecordInvalid
Validation failed: Start date can't be blank
Following is the two schema I am using, for #mess its MessType model and for #mess_price its MessPrice model:
MessType
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| mess_id | int(11) | NO | PRI | NULL | auto_increment |
| mess_name | varchar(255) | NO | | NULL | |
| mess_capacity | int(11) | NO | | NULL | |
| start_date | date | No | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
MessPrice
+-------------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| breakfast_charge | float | YES | | NULL | |
| lunch_charge | float | YES | | NULL | |
| dinner_charge | float | YES | | NULL | |
| monthly_charge | float | YES | | NULL | |
| semesterly_charge | float | YES | | NULL | |
| rebate | float | YES | | NULL | |
| start_date | date | YES | | NULL | |
| end_date | date | YES | | NULL | |
| mess_id | int(11) | YES | MUL | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+-------------------+----------+------+-----+---------+----------------+
I recommend installing the awesome_print gem - https://github.com/michaeldv/awesome_print
# add to Gemfile
gem 'awesome_print'
# install
bundle
Then the first thing in your controller action do
logger.debug " -----------"
logger.ap params
logger.debug " -----------"
Check your log file log/development.log for the output, it could be the params are coming across correctly but not what you expect?
some of the attributes might be nested in another hash key and need to be accessed via params[:something][:xyz]
I would also recommend making the code more readable and running it in a transaction
#mess.mess_name = params[:mess_name_update]
#mess.mess_capacity = params[:mess_capacity_update]
#mess_price.breakfast_charge = params[:mess_breakfast_charge_update]
#mess_price.lunch_charge = params[:mess_lunch_charge_update]
#mess_price.dinner_charge = params[:mess_dinner_charge_update]
#mess_price.monthly_charge = params[:mess_monthly_charge_update]
#mess_price.semesterly_charge = params[:mess_semesterly_charge_update]
#mess_price.start_date = params[:start_date_update]
#mess_price.end_date = params[:end_date_update]
#mess_price.rebate = params[:rebate_update]
# NOTE: an alternative to above is to name your html input fields the rails way
# so that params are sent in a nested hash, i.e. "mess_price[start_date]" -> params[:mess_price][:start_date]
# then you can do #mess_price.update_attributes(params[:mess_price])
# using form_for html helper will automatically apply this style of naming to html input fields
Mess.transaction do
# you might want save! vs save - depends if you show validation error in the UI or not
if #mess.save && #mess_price.save
flash[:success] = "Mess Details Updated Successfully!!"
else
# don't save changes, show validation errors
raise ActiveRecord::Rollback
flash[:error] = "Some Error! Please Try Again!"
end
end

Cucumber Feature - data population handling Array

I have a ActiveRecord in Rails that has a
serialize :file_names, Array
field
In my cucumber details.feature I have something like the following:
Given the following dist_details exist
| effective_date | company_id | file_names | sent_at |
| 2012-03-01 | 1 | test1.pdf,test2.pdf | 2012-04-01 13:00:00 |
| 2012-04-01 | 2 | test3.pdf,test4.pdf | 2012-04-01 14:00:00 |
when I run my cucumber test I get the following error:
Attribute was supposed to be a Array, but was a String
(ActiveRecord::SerializationTypeMismatch)
./features/step_definitions/pickle_steps.rb:15:in `/^the following ((
How to auto populate for array values for file_names test1.pdf,test2.pdf??

Resources