Created
September 22, 2009 00:05
-
-
Save bear454/190642 to your computer and use it in GitHub Desktop.
Some generic rspec steps I use for unit testing ActiveRecord models with cucumber.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# clear a table | |
Given /^I have no (\S+)$/ do |table| | |
ActiveRecord::Base.connection.execute "DELETE FROM `#{table.tableize}`" | |
end | |
# create a new instance, as an @underscore_named_var, and @it. | |
Given /^I have a new (\S+)$/ do |model| | |
eval "@#{model.underscore} = #{model.classify}.new" | |
eval "@it = #{model.classify}.new" | |
end | |
Given /^I created a valid (\S+)$/ do |model| | |
eval "@#{model.underscore} = Factory(:#{model.underscore})" | |
end | |
# Short form test - @it - perfect for single model testing. | |
# assignments | |
When /^I set its (\S+) to "([^\"]*)"$/ do |attribute, value| | |
@it.send("#{attribute}=", value) | |
end | |
# run a method without args | |
When /^I (\S+) it$/ do |method| | |
@it.send(method) | |
end | |
# list of attributes | |
Then /^it should have the following attributes$/ do |table| | |
table.hashes.each do |hash| | |
@it.attributes.has_key?(hash['attribute']).should == true | |
end | |
end | |
# associations | |
Then /^it should (belong to a|have one|have many) (.+)$/ do |method, association| | |
@it.should.respond_to?(association) | |
end | |
# simple validity | |
Then /^it should\s+be valid$/ do | |
@it.should be_valid | |
end | |
Then /^it should not be valid$/ do | |
@it.should_not be_valid | |
end | |
# attribute value | |
Then /^its (\S+) should be "([^\"]*)"$/ do |attribute, value| | |
@it.send("#{attribute}").to_s.should == value | |
end | |
# association count | |
Then /^it should\s+have (\d+) (.+)$/ do |value, association| | |
if @it.send(association.pluralize).respond_to? :count | |
@it.send(association.pluralize).count.should == value.to_i | |
else | |
@it.send(association.pluralize).size.should == value.to_i | |
end | |
end | |
# Long form test - @underscore_named_var - no confusion when working with multiple models. | |
# assignments | |
When /^I set that (\S+) (\S+) to "([^\"]*)"$/ do |var, attribute, value| | |
eval("@#{var.underscore}").send("#{attribute}=", value) | |
end | |
# run a method without args | |
When /^I (\S+) that (\S+)$/ do |method, var| | |
eval("@#{var.underscore}").send(method) | |
end | |
# list of attributes | |
Then /^that (\S+) should have the following attributes$/ do |var, table| | |
table.hashes.each do |hash| | |
eval("@#{var.underscore}").attributes.has_key?(hash['attribute']).should == true | |
end | |
end | |
# associations | |
Then /^that (\S+) should (belong to a|have one|have many) (.+)$/ do |var, method, association| | |
eval("@#{var.underscore}").should.respond_to?(association) | |
end | |
# simple validity | |
Then /^that (\S+) should\s+be valid$/ do |var| | |
eval("@#{var.underscore}").should be_valid | |
end | |
Then /^that (\S+) should not be valid$/ do |var| | |
eval("@#{var.underscore}").should_not be_valid | |
end | |
# attribute value | |
Then /^that (\S+) (\S+) should be "([^\"]*)"$/ do |var, attribute, value| | |
eval("@#{var.underscore}").send("#{attribute}").to_s.should == value | |
end | |
# attribute is another var | |
Then /^that (\S+)'s (\S+) should be that (\S+)$/ do |var, attribute, value_var| | |
eval("@#{var.underscore}").send("#{attribute}").should == eval("@#{value_var.underscore}") | |
end | |
# association count | |
Then /^that (\S+) should\s+have (\d+) (.+)$/ do |var, value, association| | |
if eval("@#{var.underscore}").send(association.pluralize).respond_to? :count | |
eval("@#{var.underscore}").send(association.pluralize).count.should == value.to_i | |
else | |
eval("@#{var.underscore}").send(association.pluralize).size.should == value.to_i | |
end | |
end | |
# array attribute includes another var | |
Then /^that (\S+)'s (\S+) should include that (\S+)$/ do |var, attribute, value_var| | |
eval("@#{var.underscore}").send("#{attribute}").should include(eval("@#{value_var.underscore}")) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Feature: FinishedGood unit test | |
In order to ensure stability, and learn BDD | |
As a developer | |
I want to validate the behavior of the Bin class | |
Background: | |
Given I have no finished_goods | |
Scenario: Attributes | |
Given I have a new finished_good | |
Then it should have the following attributes | |
|attribute | | |
|part_number | | |
|production_number | | |
And it should belong to a slot | |
And it should have one bin | |
Scenario Outline: Validations | |
Given I have a new finished_good | |
When I set its part_number to "<part_number>" | |
And I set its production_number to "<production_number>" | |
Then it should <not> be valid | |
Examples: | |
| part_number | production_number | not | explanation | | |
| | | not | part_number & production_number should both be required | | |
| 478410 | | not | '' | | |
| | 123456 | not | '' | | |
| 478410 | 123456 | | valid part_number & production_number | | |
| 478410 | abc | not | production_number must be only numbers | | |
| 478410 | -1 | not | production_number must be only numbers | | |
Scenario: Unique Production Numbers | |
Given I have a new finished_good | |
When I set its part_number to "part" | |
And I set its production_number to "1" | |
And I save it | |
Then it should be valid | |
Given I have a new finished_good | |
When I set its part_number to "part" | |
And I set its production_number to "1" | |
Then it should not be valid | |
When I set its production_number to "2" | |
Then it should be valid | |
Scenario: Bin Assignment and Unassignment | |
Given I created a valid bin | |
And I created a valid finished_good | |
When I place that finished_good in that bin | |
Then that finished_good's bin should be that bin | |
And that bin should have 1 slot | |
And that bin should have 1 binnable | |
And that bin's binnables should include that finished_good | |
When I remove that finished_good from a bin | |
Then that finished_good's bin should be blank | |
And that bin should have 0 slots | |
And that bin should have 0 binnables | |
Scenario: Bin Assignment By Label | |
Given I created a valid bin | |
And I created a valid finished_good | |
When I place that finished_good in that bin using the label | |
Then that finished_good's bin should be that bin | |
And that finished_good's bin_label should be that bin's label | |
And that bin should have 1 slot | |
And that bin should have 1 binnable | |
And that bin's binnables should include that finished_good |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment