Test failing on creating factory - ruby-on-rails

I have a problem concerning testing with factorygirl:
First some code:
customesr_spec.rb:
require 'spec_helper'
describe "customers" do
describe "signup" do
#FactoryGirl.find_definitions
user = FactoryGirl.create(:signup_customer)
it "has right data" do
visit signup_path
fill_in :id, :with => 2110001
fill_in :name, :with => "AVK POLSKA Sp. zo.o."
fill_in :email, :with => "my.email#provider.be"
fill_in :email_confirmation, :with => "my.email#provider.be"
click_button "Create account"
page.should have_content("Fireprotection")
end
end
factories.rb
FactoryGirl.define do
factory :signup_customer, class: Customer do
id = 2110001
name = "AVK POLSKA Sp. zo.o."
email = ""
address_1 = "ul. Jakubowska 1"
address_2 = "Pniewy 62-045"
zipcode = 62
city = "Pniewy"
currency = "PLN"
country_id = "PL"
contact_person_id = "AZU"
reset_token = nil
reset_token_init = nil
end
end
This is the error that I get when running that test:
Running tests with args ["--drb", "-f", "progress", "-r", "c:/Ruby193/lib/ruby/gems/1.9.1/gems/guard-rspec-2.1.1/lib/guard/rspec/formatter.rb",
"-f", "Guard::RSpec::Formatter", "--out", "/dev/null", "--failure-exit-code", "2", "spec"]...
<-- take tuple(1); slave.run...
09:17:40 - ERROR - Guard::RSpec failed to achieve its <start>, exception was:
[#73C9383A03A6] DRb::DRbUnknownError: ActiveRecord::
[#73C9383A03A6] c:/Ruby193/lib/ruby/1.9.1/drb/drb.rb:1095:in method_missing'
[#73C9383A03A6] c:/Ruby193/lib/ruby/gems/1.9.1/gems/guard-rspec-2.1.1/lib/guard/rspec/runner.rb:124:inrun_via_drb'
Do I have to put a require somewhere? What am I missing here?

First I have to mention this:
I was using Guard with Rspec and Spork on my windows pc.
-> I added Spork to have faster tests once guard and rspec were running.
What I did to solve the problem (with thanks to the freenode #RubyOnRails channel.:
user = FactoryGirl.create(:signup_customer)
#This is wrong! Has to be:
let(:user) {FactoryGirl.create(:signup_customer)}

Did you init the spork? You should have some Spork.prefork blocks in your code so it know what it can keep in memory for the entire time and what it can reload upon each run. I don't see your rspec files here with the spork blocks defined.

Related

Rails + Capybara + Braintree—how to feature test BT's Hosted Fields?

TL;DR—How do I access fields within Braintree's Hosted Fields' iframes?
I want to test a UX flow of paying a donation through Braintree. This is my code so far:
require "rails_helper"
RSpec.feature "Donation Module", type: :feature do
scenario "Public visitor creates a new donation" do
#load page
website = create(:website)
Capybara.current_session.driver.header 'Referer', website.website
visit "/donate?t=#{website.public_token}&frame=1"
#verify page loaded
expect(page).not_to have_content("Are you sure you're installing this on the correct website?")
#fill page 1
find("input[value='20']").click
#go to page 2
find("#credit-details").click
#verify page 2 content is loaded
expect(find(".total-cost-text")).to be_visible
#fill page 2
fill_in 'First Name', with: 'Leeroy'
fill_in 'Last Name', with: 'Jenkins'
fill_in 'Email for receipt', with: 'new_donor#email.com'
within_frame('#braintree-hosted-field-number') do
fill_in '#credit-card-number', with: '4111-1111-1111-1111'
end
within_frame('#braintree-hosted-field-expirationDate') do
fill_in '#expiration', with: '09/19'
end
within_frame('#braintree-hosted-field-cvv') do
fill_in '#cvv', with: '123'
end
find('Make payment').click
# expect to make a new user, new donation, new receipt, email receipt
end
end
Currently, it's breaking at the first within_frame saying Capybara::NotSupportedByDriverError:
Capybara::Driver::Base#within_frame
How do I access fields inside BT's iframes?
Well, I am writing here not exactly an answer to this question, but rather corrections to the question, as I was in the similar situation and was facing similar errors such as Selenium::WebDriver::Error::NoSuchFrameError: Unable to locate frame: #braintree-hosted-field-number and Test::Unit::Capybara::ElementNotFound: Unable to find field "#credit-card-number".
The within_frame should have the following format (the #-sign for ID should be removed from both):
within_frame('braintree-hosted-field-number') do
fill_in 'credit-card-number', :with => number
end
And in order to use the selenium driver in Test::Unit, I used the following helper:
def js
Capybara.current_driver = Capybara.javascript_driver
yield
Capybara.current_driver = Capybara.use_default_driver
end
And then wrapped my tests in it:
class SomeTest < ActionDispatch::IntegrationTest
test "should ..." do
js do
within_frame('braintree-hosted-field-number') do
fill_in 'credit-card-number', :with => number
end
# ...
end
end
Hopefully, someone will find it useful while using Unit Tests.
Seems like you're using the rack_test driver? That doesn't support JS or frames so braintree isn't going to work with that. You need to switch to one of the real browser drivers like selenium, capybara-webkit, or poltergeist.

Poltergeist capybara view testing taking long time when running all test in spec in ruby on rails

I'm using capybara with poltergeist for rspec testing in ruby on rails.
We're using stripe for payment gateway.
Request to stripe in already stub and real http connections are disabled.
However when I run the spec file alone.
It passes with the following test result.
Can add billing information to my account
Finished in 14.27 seconds (files took 4.84 seconds to load)
1 example, 0 failures
But when I run all the test it fails due to long time delay for response and I'm getting this output.
Failures:
My code-
scenario "Can add billing information to my account", js: true do
within "#top-menu" do
click_link "ACCOUNT"
end
click_link "Billing"
click_link "Add billing information"
within "#subscription" do
fill_in "Name", with: "John Smith"
fill_in "Email", with: "john.smith#example.com"
fill_in "Phone number", with: "0443 123123"
fill_in "Company", with: "Cribber"
fill_in "Street address", with: "14 Somewhere Street"
fill_in "City", with: "Brisbane"
fill_in "Postcode / Zip", with: "4000"
fill_in "State", with: "QLD"
select "Australia", from: "Country"
select "Australian Dollar", from: "Currency"
fill_in "Card number", with: "4242424242424242"
select Time.now.year + 1, from: "card_expiry_year"
fill_in "CVC", with: "123"
check "subscription_billing_attributes_terms_of_service"
end
click_button "Subscribe"
expect(page).to have_content("Thank you for subscribing to Cribber!")
end
I added sleep(200) after click_button "Subscribe".But it showed the following result.
Can anyone please help?
Thanks in advance for helping.
I tried changing http://localhost to http://127.0.0.1 in .env.test file but I got these error.
The strange thing is that if I run the spec file alone or the feature folder for view spec then I get no error message and the test pass.But if I run
rspec spec/ then the test fails.
Can anyone help?
It looks like the error is explained in your screenshot - while localhost and 127.0.0.1 point to the same place they are technically different origins. You need to find where you're setting the app to make a request to http://localhost/... and change it to 127.0.0.1

Capybara/Poltergeist causing unexpected results in request spec in Rails 3.2 app

Is there an alternative I can use to Capybara's save_and_open_page? I have written a request spec that test the user signup form. Upon successful signup, which this test should yield, this test should be passing, but it's not.
Yet when I do save_and_open_page at the end, all the form fields are blank. I can mirror this in the development environment doing the data entry myself and everything passes, so I am struggling to understand what's wrong. I can't afford to let this go because obviously this is a critical test.
I've only been doing TDD for a little while, so any tips or tricks about this stack (Capybara and Poltergeist) would be helpful. I am not having similar difficulties in other request specs using the same stack.
Here is the request spec:
it 'allows a user to subscribe', js: true do
visit root_url
click_link "Pricing"
click_button "Subscribe", match: :first
fill_in "First name", with: "Marky"
fill_in "Last name", with: "Mark"
fill_in "Company", with: "The Funky Bunch"
fill_in "subscription_email", with: "marky.mark#thefunkybunch.com"
fill_in "Password", with: "MyString"
fill_in "Password confirmation", with: "MyString"
fill_in "Credit Card Number", with: "4242424242424242"
fill_in "Security Code on Card (CVV)", with: "123"
select "July", from: "Card Expiration"
select "2014", from: "card_year"
check "chkACCEPT"
click_button "Subscribe to myapp"
# The line below fails, but a save_and_open page just shows a blank form
current_path.should_not eq(new_subscription_path)
page.should have_content("Thank You")
end
Poltergeist provides screenshots which can be used instead of save_and_open_page:
save_screenshot('/path/to/file.png', :full => true)
However your comment "# The line below fails" seems very consistent to "save_and_open page just shows a blank form".

Capybara detecting CSS ID model ID with find('')

I am currently working on an intranet for work which will include creating invoices.
While writing tests I am using Rspec and Capybara for testing and while running a request test I want to create an invoice and add a couple of items to make sure the vat is calculated correctly.
However I seem to be having an issue where Capybara finds the first #item_price and tries to match that instead of the newly created item.
Below is some of the code in my request test.
...
...
# Laptop for 369.96 inc vat
click_link "Add Item"
fill_in "Quantity", :with => 1
fill_in "Price", :with => 369.96
fill_in "Description", :with => "laptop for 369"
click_button "Create Item"
page.should have_content("Item was successfully created")
find('#item_quantity').should have_content(1)
find('#item_price').should have_content(369.96)
find('#item_description').should have_content("laptop for 369")
find('#item_total').should have_content(369.96)
find('#invoice_subtotal').should have_content(308.30)
find('#invoice_vat').should have_content(61.66)
find('#invoice_total').should have_content(369.96)
# Laptop for 337.53 inc vat
click_link "Add Item"
fill_in "Quantity", :with => 1
fill_in "Price", :with => 337.53
fill_in "Description", :with => "laptop for 337"
click_button "Create Item"
page.should have_content("Item was successfully created")
find('#item_quantity').should have_content(1)
### the offending code below
find('#item_price').should have_content(337.53)
###
find('#item_description').should have_content("laptop for 337")
...
...
I would like to be able to add #item_price_2 where 2 would be the item ID. When doing this and changing find('#item_price').should have_content(337.53) to find('#item_price_#{invoice.id}').should have_content(337.53) I get the exception:
Nokogiri::CSS::SyntaxError:
unexpected '#' after '[#<Nokogiri::CSS::Node:0x007f991889f270 #type=:CONDITIONAL_SELECTOR, #value=[#<Nokogiri::CSS::Node:0x007f991889f3d8 #type=:ELEMENT_NAME, #value=["*"]>, #<Nokogiri::CSS::Node:0x007f991889f9c8 #type=:ID, #value=["#item_price_"]>]>]'
Also changing it to double quotes "#item_price_{invoice.id}" gives me this exception:
undefined local variable or methodinvoice' for #
I'm guessing this is because I'm not using FactoryGirl so invoice isn't set as a method yet. I'm not using FactoryGirl in this occasion because I would like to test how the user would interact with the invoice and items forms.
What would be the best way to fix this problem? Is there a better way of doing what I am trying to do?
Many thanks in advance!
Your guess about invoice not being set yet is correct. After your form is submitted you could set it:
invoice = Invoice.last
Then your specs should pass.

Factory Girl created objects not clearing in between tests?

I'm being held up implementing tests with a slight confusion. With User.create I can create and save in multiple tests:
should "test something" do
u1 = User.create(:first_name => "Fred", :last_name => "Flintstone")
assert true
end
should "test something else" do
u1 = User.create(:first_name => "Fred", :last_name => "Flintstone")
assert true
end
but using Factory.create, it throws a ActiveRecord duplicate entry error:
should "test something" do
Factory.create(:amanda_levy)
assert true
end
should "test something else" do
Factory.create(:amanda_levy)
assert true
end
Error: "ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry"
What gives?
Do you have the the following line in your spec_helper:
config.use_transactional_fixtures = true
That tells rspec/ test::unit to start a transaction at the start
of every test case, and issue a rollback when it's over.

Resources