Created
April 11, 2013 21:09
-
-
Save link82/5367186 to your computer and use it in GitHub Desktop.
I'm playing with rspec and factory girl but I'm stuck with a validation error for duplicate device entries while testing even if it should create different apps and devices for every test
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
class Device < ActiveRecord::Base | |
attr_accessible :app_id, :app_version, :locale, :push_token, :uid, :alias | |
belongs_to :app | |
validates_presence_of :app | |
validates :push_token, | |
:presence => true, :uniqueness => {:scope => :app_id, :message => 'already registered'} | |
validates_presence_of :locale | |
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
FactoryGirl.define do | |
factory :app do | |
environment 'production' | |
sequence(:name){|n| "Test#{n}Application"} | |
os 'iOS' | |
version '1.0' | |
sequence(:bundle_identifier){|n| "com.getting_angry.Test#{n}Application"} | |
certificate_expire_at nil | |
app_administrator '[email protected]' | |
#created 10 devices | |
after(:create) do |app, evaluator| | |
FactoryGirl.create_list(:device, 8, app: app) | |
FactoryGirl.build_list(:en_US_device, 2, app: app) | |
end | |
factory :app_with_notifications do | |
ignore do | |
notifications_count 5 #default nr | |
end | |
after(:create) do |app, evaluator| | |
FactoryGirl.create_list(:notification, evaluator.notifications_count, app: app) | |
end | |
end | |
factory :app_with_broadcast_notifications do | |
ignore do | |
notifications_count 10 #default nr | |
end | |
after(:create) do |app, evaluator| | |
FactoryGirl.create_list(:broadcast_notification, evaluator.notifications_count, app: app) | |
end | |
end | |
end | |
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
FactoryGirl.define do | |
factory :device do |d| | |
d.app_version '1.0' | |
d.locale 'it_IT' | |
d.sequence(:push_token){|p| 'raad0saadw0wdi0ae9a#{p}'} | |
d.uid 'test_user' | |
d.association :app, factory: :app | |
factory :en_US_device do |e| | |
e.locale 'en_US' | |
end | |
end | |
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
require "spec_helper" | |
describe App do | |
it "should limit notifications sent" do | |
a = FactoryGirl.create(:app_with_notifications,notifications_count: 80) | |
a.notifications.length.should == 80 | |
a.should_receive(:send_notifications).exactly(2).times | |
a.check_notifications(true) | |
end | |
it "should generate 'broadcast' notifications" do | |
a = FactoryGirl.create(:app_with_broadcast_notifications, notifications_count: 1) | |
a.notifications.length.should == 1 | |
data = a.explode_notification(a.notifications.first) | |
data.length.should == a.devices.count | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Couple of other ideas:
Future-proof your app and use
strong_parameters
overattr_accessible
:https://github.com/rails/strong_parameters
Use the FactoryGirl shorthand syntax methods:
app = create(:app_with_notifications, notifications_count: 80)