Commit 61fc8101 by ivan Lan

Finish adding factory_girl

parent b303d471
require 'weather-model/version'
module WeatherModel
Dir[File.dirname(__FILE__) + '/weather-model/*.rb'].each { |file| require file }
require 'factory_girl_rails'
def self.included base
WeatherModel::SourceSchema.sorts.each { |sort| Object.const_set(sort, Module.new)}
WeatherModel::SourceSchema.constants.each do |source|
......
......@@ -26,6 +26,7 @@ module WeatherModel
son: nil,
},
factory_name: :forecast_shanghai_ten,
factory: {
datetime: '2017-05-16',
cityname: 'shanghai',
......
......@@ -8,7 +8,8 @@ module WeatherModel
after_save :update_ohm
def self.use_settings settings
self.table_name = settings[:table_name]
self.define_ohm settings
define_ohm settings
define_factory settings
end
......@@ -16,6 +17,15 @@ module WeatherModel
const_set('Ohm', Class.new(SourceOhm)).use_settings(settings)
end
def self.define_factory settings
model_name = self
::FactoryGirl.define do
factory settings[:factory_name], class: model_name do
settings[:factory].each_pair{ |key, val| send(key, val) }
end
end
end
def get_ohm
self.class.const_get('Ohm')
end
......
require "bundler/setup"
require "weather-model"
require 'rails'
require 'support/factory_girl'
RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
......
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
# RSpec without Rails
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.before(:suite) do
FactoryGirl.find_definitions
end
end
\ No newline at end of file
......@@ -22,8 +22,19 @@ RSpec.describe WeatherModel do
expect(WeatherModel::VERSION).not_to be nil
end
describe 'test factory_girl' do
before do
@obj = create(:forecast_shanghai_ten)
end
it 'Test factory_girl' do
expect(@obj.class).to eq(Forecast::ShanghaiTen), 'factory_girl 生成成功'
end
end
it 'Forecast' do
@expire = 2
@expire = 1
Forecast::ShanghaiTen::Ohm.expire @expire
mysql_obj = Forecast::ShanghaiTen.create({
datetime: '2017-05-16',
......@@ -42,8 +53,7 @@ RSpec.describe WeatherModel do
# 'ohm 对象 同步 mysql对象更新'
mysql_obj.update(temp_low: 99)
expect(Forecast::ShanghaiTen::Ohm.all.first.temp_low).to eq('99'), 'ohm 对象 同步 mysql对象更新'
sleep 3
sleep 1.5
expect(Forecast::ShanghaiTen::Ohm.all.count).to eq(0), 'TTL过期对象删除成功'
end
......
......@@ -32,12 +32,13 @@ Gem::Specification.new do |spec|
# Object-Hash Mapping for Redis
# https://github.com/soveran/ohm
spec.add_dependency "ohm"
spec.add_dependency 'ohm'
# Ohm plugin that exposes ttl control over modules
# https://github.com/joseairosa/ohm-expire
spec.add_dependency "ohm-expire"
spec.add_dependency 'ohm-expire'
spec.add_dependency "activerecord"
spec.add_dependency 'activerecord'
spec.add_dependency 'factory_girl_rails'
spec.add_development_dependency "bundler", "~> 1.14"
spec.add_development_dependency "rake", "~> 10.0"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment