Merge pull request #16 from mneorr/testing

Adding model specs
This commit is contained in:
Jarred Sumner
2012-11-27 11:49:53 -08:00
21 changed files with 307 additions and 1416 deletions

2
.gitignore vendored
View File

@@ -17,3 +17,5 @@
config/settings.local.yml
config/settings/*.local.yml
config/environments/*.local.yml
*.sublime-workspace

3
.rspec Normal file
View File

@@ -0,0 +1,3 @@
-fp
--color
--require spec_helper

View File

@@ -4,6 +4,7 @@ gem 'rails', '3.2.8'
group :development do
gem 'sqlite3'
gem 'pry-rails'
end
group :production do
@@ -20,6 +21,11 @@ group :assets do
gem 'uglifier', '>= 1.0.3'
end
group :development, :test do
gem 'rspec-rails', '~> 2.0'
gem 'shoulda'
end
# jQuery
gem 'jquery-rails'

View File

@@ -34,6 +34,7 @@ GEM
rest-client (~> 1.6.1)
arel (3.0.2)
builder (3.0.3)
coderay (1.0.8)
coffee-rails (3.2.2)
coffee-script (>= 2.2.0)
railties (~> 3.2.0)
@@ -42,6 +43,7 @@ GEM
execjs
coffee-script-source (1.3.3)
daemons (1.1.9)
diff-lcs (1.1.3)
erubis (2.7.0)
eventmachine (1.0.0)
execjs (1.4.0)
@@ -58,11 +60,18 @@ GEM
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
method_source (0.8.1)
mime-types (1.19)
multi_json (1.3.6)
multi_xml (0.2.2)
pg (0.14.1)
polyglot (0.3.3)
pry (0.9.10)
coderay (~> 1.0.5)
method_source (~> 0.8)
slop (~> 3.3.1)
pry-rails (0.2.2)
pry (>= 0.9.10)
rack (1.4.1)
rack-cache (1.2)
rack (>= 0.4)
@@ -92,11 +101,29 @@ GEM
json (~> 1.4)
rest-client (1.6.7)
mime-types (>= 1.16)
rspec-core (2.12.0)
rspec-expectations (2.12.0)
diff-lcs (~> 1.1.3)
rspec-mocks (2.12.0)
rspec-rails (2.12.0)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 2.12.0)
rspec-expectations (~> 2.12.0)
rspec-mocks (~> 2.12.0)
sass (3.2.1)
sass-rails (3.2.5)
railties (~> 3.2.0)
sass (>= 3.1.10)
tilt (~> 1.3)
shoulda (3.3.2)
shoulda-context (~> 1.0.1)
shoulda-matchers (~> 1.4.1)
shoulda-context (1.0.1)
shoulda-matchers (1.4.1)
activesupport (>= 3.0.0)
slop (3.3.3)
sprockets (2.1.3)
hike (~> 1.2)
rack (~> 1.0)
@@ -126,9 +153,12 @@ DEPENDENCIES
coffee-rails (~> 3.2.1)
jquery-rails
pg
pry-rails
rails (= 3.2.8)
rails_config
rspec-rails (~> 2.0)
sass-rails (~> 3.2.3)
shoulda
sqlite3
therubyracer
thin

View File

@@ -11,7 +11,7 @@ class Order < ActiveRecord::Base
@order.name = options[:name]
@order.user_id = options[:user_id]
@order.price = options[:price]
@order.number = Order.next_order_number || 1
@order.number = Order.next_order_number
@order.save!
@order
@@ -20,7 +20,7 @@ class Order < ActiveRecord::Base
# After authenticating with Amazon, we get the rest of the details
def self.postfill!(options = {})
@order = Order.find_by_uuid!(options[:callerReference])
@order.token = options[:tokenID]
@order.token = options[:tokenID]
if @order.token.present?
@order.address_one = options[:addressLine1]
@order.address_two = options[:addressLine2]
@@ -38,7 +38,11 @@ class Order < ActiveRecord::Base
end
def self.next_order_number
Order.order("number DESC").limit(1).first.number.to_i + 1 if Order.count > 0
if Order.count > 0
Order.order("number DESC").limit(1).first.number.to_i + 1
else
1
end
end
def generate_uuid!

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,7 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
one:
user_id: 1
token: MyString
transaction_id: MyString
address_one: MyString
@@ -11,10 +12,12 @@ one:
country: MyString
status: MyString
number: MyString
uuid: MyString
name: Marin
price: 123.05
uuid: ec781fa2-c5e6-4af9-8049-4dee15a85296
two:
token: MyString
user_id: 2
transaction_id: MyString
address_one: MyString
address_two: MyString

View File

@@ -1,7 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
one:
email: MyString
email: mneorr@gmail.com
id: 1
two:
email: MyString
id: 2

207
spec/models/order_spec.rb Normal file
View File

@@ -0,0 +1,207 @@
describe Order do
context "attributes" do
[:address_one, :address_two, :city, :country, :number, :state, :status,
:token, :transaction_id, :zip, :shipping, :tracking_number, :name,
:price, :phone, :expiration
].each do |property|
it { should allow_mass_assignment_of property }
end
it { should_not allow_mass_assignment_of :uuid }
it "generates UUID before validation on_create" do
# TODO
end
it { Order.primary_key.should == 'uuid' }
end
context "class methods" do
describe ".prefill!" do
before do
@options = {
name: 'marin',
user_id: 12983,
price: 123.12
}
@order = Order.prefill!(@options)
end
it "sets the name" do
@order.name.should == @options[:name]
end
it "sets user_id" do
@order.user_id.should == @options[:user_id]
end
it "sets the price" do
@order.price.should == @options[:price]
end
it "saves" do
Order.any_instance.should_receive :save!
Order.prefill!(@options)
end
it "uses the right order number" do
numbah = Order.next_order_number
Order.prefill!(@options).number.should == numbah
end
end
describe ".postfill!" do
fixtures :orders
before do
@options = {
callerReference: 'ec781fa2-c5e6-4af9-8049-4dee15a85296',
tokenID: 128736127863,
addressLine1: '102 Fake address',
addressLine2: 'Apt 12, 3rd fl',
city: 'Mountain View',
state: 'CA',
status: 'IN PROGRESS',
zip: '94041',
phoneNumber: '650 219 9382',
country: 'United States',
expiry: (Time.now + 99999).to_s
}
Order.stub!(:find_by_uuid!).and_return orders(:one)
@order = Order.postfill!(@options)
end
it "finds order by uuid" do
Order.should_receive(:find_by_uuid!)
Order.postfill!
end
it "sets the token" do
@order.token.should == @options[:tokenID]
end
it "checks if token is present" do
Order.postfill!.should be_nil
end
it "sets addresses" do
@order.address_one.should == @options[:addressLine1]
@order.address_two.should == @options[:addressLine2]
end
it "sets city" do
@order.city.should == @options[:city]
end
it "sets state" do
@order.state.should == @options[:state]
end
it "sets status" do
@order.status.should == @options[:status]
end
it "sets zip" do
@order.zip.should == @options[:zip]
end
it "sets country" do
@order.country.should == @options[:country]
end
it "sets phone" do
@order.phone.should == @options[:phoneNumber]
end
it "sets expiration" do
@order.expiration.should == Date.parse(@options[:expiry])
end
it "saves" do
Order.any_instance.should_receive :save!
Order.postfill!(@options)
end
end
describe ".next_order_number" do
it "gives the next number" do
ActiveRecord::Relation.any_instance.stub(:first).and_return(stub( number: 1 ))
Order.next_order_number.should == 2
end
context "no orders" do
before do
ActiveRecord::Relation.any_instance.stub(:first).and_return(nil)
Order.stub!(:count).and_return(0)
end
it "doesn't break if there's no orders" do
expect { Order.next_order_number }.to_not raise_error
end
it "returns 1 if there's no orders" do
Order.next_order_number.should == 1
end
end
end
describe ".percent" do
it "calculates the percent based on #goal and #current" do
Order.stub(:current).and_return(6.2)
Order.stub(:goal).and_return(2.5)
Order.percent.should == 2.48 * 100
end
end
describe ".goal" do
it "returns the project goal from Settings" do
Order.goal.should == Settings.project_goal
end
end
describe ".revenue" do
it "multiplies the #current with price from Settings" do
Order.stub(:current).and_return(4)
Settings.stub(:price).and_return(6)
Order.revenue.should == 24
end
end
describe ".current" do
it "returns the number of orders with valid token / that have been postfilled" do
Order.delete_all
order = Order.prefill!(name: 'marin', user_id: 1, price: 123.21)
Order.current.should == 0
Order.postfill!(callerReference: order.uuid, tokenID: '1232', expiry: '2015-12-24')
Order.current.should == 1
end
end
end
describe "validators" do
it { should validate_presence_of :name }
it { should validate_presence_of :price }
it { should validate_presence_of :user_id }
end
end

6
spec/models/user_spec.rb Normal file
View File

@@ -0,0 +1,6 @@
describe User do
it { should have_many :orders }
it { should respond_to :email }
end

38
spec/spec_helper.rb Normal file
View File

@@ -0,0 +1,38 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
end

View File

View File

@@ -1,18 +0,0 @@
require 'test_helper'
class PreorderControllerTest < ActionController::TestCase
test "should get index" do
get :index
assert_response :success
end
test "should get checkout" do
get :checkout
assert_response :success
end
test "should get get_excited" do
get :get_excited
assert_response :success
end
end

View File

@@ -1,12 +0,0 @@
require 'test_helper'
require 'rails/performance_test_help'
class BrowsingTest < ActionDispatch::PerformanceTest
# Refer to the documentation for all available options
# self.profile_options = { :runs => 5, :metrics => [:wall_time, :memory]
# :output => 'tmp/performance', :formats => [:flat] }
def test_homepage
get '/'
end
end

View File

@@ -1,13 +0,0 @@
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
fixtures :all
# Add more helper methods to be used by all tests here...
end

View File

View File

@@ -1,4 +0,0 @@
require 'test_helper'
class PreorderHelperTest < ActionView::TestCase
end

View File

@@ -1,7 +0,0 @@
require 'test_helper'
class OrderTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

@@ -1,7 +0,0 @@
require 'test_helper'
class UserTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end