Commit 61fc8101 by ivan Lan

Finish adding factory_girl

parent b303d471
require 'weather-model/version' require 'weather-model/version'
module WeatherModel module WeatherModel
Dir[File.dirname(__FILE__) + '/weather-model/*.rb'].each { |file| require file } Dir[File.dirname(__FILE__) + '/weather-model/*.rb'].each { |file| require file }
require 'factory_girl_rails'
def self.included base def self.included base
WeatherModel::SourceSchema.sorts.each { |sort| Object.const_set(sort, Module.new)} WeatherModel::SourceSchema.sorts.each { |sort| Object.const_set(sort, Module.new)}
WeatherModel::SourceSchema.constants.each do |source| WeatherModel::SourceSchema.constants.each do |source|
......
...@@ -26,6 +26,7 @@ module WeatherModel ...@@ -26,6 +26,7 @@ module WeatherModel
son: nil, son: nil,
}, },
factory_name: :forecast_shanghai_ten,
factory: { factory: {
datetime: '2017-05-16', datetime: '2017-05-16',
cityname: 'shanghai', cityname: 'shanghai',
......
...@@ -8,7 +8,8 @@ module WeatherModel ...@@ -8,7 +8,8 @@ module WeatherModel
after_save :update_ohm after_save :update_ohm
def self.use_settings settings def self.use_settings settings
self.table_name = settings[:table_name] self.table_name = settings[:table_name]
self.define_ohm settings define_ohm settings
define_factory settings
end end
...@@ -16,6 +17,15 @@ module WeatherModel ...@@ -16,6 +17,15 @@ module WeatherModel
const_set('Ohm', Class.new(SourceOhm)).use_settings(settings) const_set('Ohm', Class.new(SourceOhm)).use_settings(settings)
end 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 def get_ohm
self.class.const_get('Ohm') self.class.const_get('Ohm')
end end
......
require "bundler/setup" require "bundler/setup"
require "weather-model" require "weather-model"
require 'rails' require 'rails'
require 'support/factory_girl'
RSpec.configure do |config| RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure # 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 ...@@ -22,8 +22,19 @@ RSpec.describe WeatherModel do
expect(WeatherModel::VERSION).not_to be nil expect(WeatherModel::VERSION).not_to be nil
end 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 it 'Forecast' do
@expire = 2 @expire = 1
Forecast::ShanghaiTen::Ohm.expire @expire Forecast::ShanghaiTen::Ohm.expire @expire
mysql_obj = Forecast::ShanghaiTen.create({ mysql_obj = Forecast::ShanghaiTen.create({
datetime: '2017-05-16', datetime: '2017-05-16',
...@@ -42,8 +53,7 @@ RSpec.describe WeatherModel do ...@@ -42,8 +53,7 @@ RSpec.describe WeatherModel do
# 'ohm 对象 同步 mysql对象更新' # 'ohm 对象 同步 mysql对象更新'
mysql_obj.update(temp_low: 99) mysql_obj.update(temp_low: 99)
expect(Forecast::ShanghaiTen::Ohm.all.first.temp_low).to eq('99'), 'ohm 对象 同步 mysql对象更新' 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过期对象删除成功' expect(Forecast::ShanghaiTen::Ohm.all.count).to eq(0), 'TTL过期对象删除成功'
end end
......
...@@ -32,12 +32,13 @@ Gem::Specification.new do |spec| ...@@ -32,12 +32,13 @@ Gem::Specification.new do |spec|
# Object-Hash Mapping for Redis # Object-Hash Mapping for Redis
# https://github.com/soveran/ohm # https://github.com/soveran/ohm
spec.add_dependency "ohm" spec.add_dependency 'ohm'
# Ohm plugin that exposes ttl control over modules # Ohm plugin that exposes ttl control over modules
# https://github.com/joseairosa/ohm-expire # 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 "bundler", "~> 1.14"
spec.add_development_dependency "rake", "~> 10.0" 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