Cucumber test question

67 views
Skip to first unread message

Vell

unread,
Feb 11, 2014, 3:43:40 PM2/11/14
to rubyonra...@googlegroups.com
Hello all,

I am trying to understand when I try to run a test in cucumber what I am missing that it is failing. I am using FactoryGirl to create my records and using cucumber to test my logic however I know that I am doing something wrong in my testing I just can't seem to figure out what. Here is what I have:

Feature:

Feature: All features related to staff info
  Scenario: notification for empty shirt size
    Given there is at least 1 staff info record
    When the shirt size field is empty
    And the shirt size check is run
    Then the system should send an email notification

Steps:

Given(/^there is at least (\d+) staff info record$/) do |arg1|
  assert_difference('StaffInfo.count', 1) do
    @staff_info = FactoryGirl.create :staff_info
  end
end

When(/^the shirt size field is empty$/) do
  assert @staff_info.update_attribute(:shirt_size, nil)
end

When(/^the shirt size check is run$/) do
  StaffInfo.check_empty_shirt_fields
end

Then(/^the system should send an email notification$/) do
  pending # express the regexp above with the code you wish you had
end

Model:

class StaffInfo < ActiveRecord::Base
  belongs_to :person
  scope :empty_shirt_fields, -> { where('shirt_size in (?)', [nil, '']) }
 
  def self.check_empty_shirt_fields
    staff_with_missing_shirt_sizes = StaffInfo.empty_shirt_fields
    debugger
    send_empty_shirt_size_notification staff_with_missing_shirt_sizes if staff_with_missing_shirt_sizes.size >  0
  end
 
  def send_empty_shirt_size_notification(staff_records)
    Mailer.missing_shirt_size(staff_records).deliver
  end
end

What is happening is, when I hit the 3rd test I did a check to see if the record was present (StaffInfo.count) and it was. But after running   StaffInfo.check_empty_shirt_fields, I did a check for the record and it was gone. I am not understanding how my record could have disappeared when I all my method was doing was checking for any records with a missing field.

Anyone have any ideas?

jsnark

unread,
Feb 12, 2014, 8:43:56 AM2/12/14
to rubyonra...@googlegroups.com

The default behavior of cucumber is to run each scenario in a transaction and then do a rollback when the scenario completes.  The behavior is specified by the DatabaseCleaner.strategy in the env.rb file/

Vell

unread,
Feb 12, 2014, 2:44:54 PM2/12/14
to rubyonra...@googlegroups.com
I'm not sure I understand. This is all one scenario so why would database cleaner  delete my record in the middle of my scenario? or am I missing something?

I create my record at the start of the scenario then run the method that should check for this record but the record but it not there after that method checks for the record. This should be in the same transaction right? Even if I place debugger anywhere in the step definitions I should still have one record right?


--
You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-talk/U7nmztmWmzA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f1a540c9-e0db-41bb-8430-41863b7eae72%40googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

Alexandru-Emil Lupu

unread,
Feb 12, 2014, 6:48:31 PM2/12/14
to rubyonra...@googlegroups.com
Hello! 
I might be wrong, but i think this is causing you problems: 

Given(/^there is at least (\d+) staff info record$/) do |arg1|
 assert_difference('StaffInfo.count', 1) do
   @staff_info = FactoryGirl.create :staff_info
 end
end

When(/^the shirt size field is empty$/) do
 assert @staff_info.update_attribute(:shirt_size, nil)
end


Most probably is because you are combining cucumber with Rails test, meaning you managed to perform a test in a test ... Those assertions might be your problem... 

I haven't met this behavior, but most probably your databse_cleaner is cleaning your test after those assertions, which is inside your scenario. 

I'd go for something like this:

# Populating my Db and Make sure i have at least one 
Given /^there is at least (\d+) staff info record$/ do |arg1|
  @staff_info = FactoryGirl.create :staff_info
  (arg1.to_i - 1).times do 
    FactoryGirl.create :staff_info
  end
end 

# Make sure my model is empty 
When /^the shirt size field is empty$/ do
 @staff_info.update_attribute(:shirt_size, nil)
end

....

In my support env.rb i would add 

World FactoryGirl::Syntax::Methods

Then use create(:staff_info) instead of full FactoryGirl.create syntax ... 

Tks. 
A.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.

Kosmas

unread,
Feb 13, 2014, 5:32:01 AM2/13/14
to rubyonra...@googlegroups.com


 Vell,

One way to be able to find out what is happening is:

a) tag your scenario with @pause, that will pause after each step.
b) Open a terminal and have a look at the test log file: tail -f log/test.log

That way you can see what is happening in the test database at each step, and when records are created, and/or the database is cleared.

P.S. It would also be a better idea to post your question in the cucumber email list if you still have problems https://groups.google.com/forum/#!forum/cukes


Vell

unread,
Feb 13, 2014, 10:33:18 AM2/13/14
to rubyonra...@googlegroups.com
Thanks Kosmas, I will try that as well as the advice from Alexandru-Emil and see if I can make some progress. Yes I am on the cukes list so I will definitely post there if I am still having issues.


--
You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-talk/U7nmztmWmzA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rubyonrails-ta...@googlegroups.com.

To post to this group, send email to rubyonra...@googlegroups.com.

Vell

unread,
Feb 13, 2014, 11:24:05 AM2/13/14
to rubyonra...@googlegroups.com
Alexandru-Emil hit was right on. Once I took out the assertion for creating the record, once I finally get down to where I am executing my method the record is still there. 


--
You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-talk/U7nmztmWmzA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rubyonrails-ta...@googlegroups.com.

To post to this group, send email to rubyonra...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages