use*_*658 2 postgresql rspec ruby-on-rails factory-bot
我正在使用带有FactoryGirl的Rails 4.1.7,Rspec 3.0.4和一个Postgresql ActiveRecord数据库。
我正在尝试测试一种找到关联模型的方法,然后更新该模型中的列。在这一点上,测试还很粗糙,但它使所有关联成为必要(我在撬动中检查)。调用该方法时,期望值将继续返回nil(默认情况下该列为nil,但应更新为日期时间)。下面是代码:
post_picture_spec.rb
require 'rails_helper'
RSpec.describe ModelName::PostPicture, :type => :model do
describe 'update_campaign method' do
context 'when the picture is tied to a campaign' do
before do
@campaign = FactoryGirl.create(:campaign)
@campaign.media << @picture1 = FactoryGirl.create(:picture, posted: true, time_scheduled: Date.parse('2014-07-15 18:00:00'), time_posted: Date.parse('2014-07-15 18:00:00'))
@campaign.media << @picture2 = FactoryGirl.create(:picture, posted: true, time_scheduled: Date.parse('2014-08-20 19:00:00'), time_posted: Date.parse('2014-08-20 19:00:00'))
end
it 'should update first item in campaign' do
# pending("FactoryGirl update issue")
ModelName::PostPicture.send(:update_campaign, @picture)
expect(@campaign.time_started).to eq(@picture.time_posted)
end
end
end
Run Code Online (Sandbox Code Playgroud)
图片工厂
FactoryGirl.define do
factory :picture do
time_approved { Date.parse('2014-08-22 17:00:00') }
time_posted { Date.parse('2014-08-22 17:00:00') }
time_scheduled { Date.parse('2014-08-22 17:00:00') }
processed true
end
end
Run Code Online (Sandbox Code Playgroud)
post_picture.rb
class ModelName::PostPicture
def self.call(id)
[collapsed]
end
private
def self.update_campaign(picture)
campaign = picture.campaign
campaign_pictures = campaign.pictures.sort_by(&:time_scheduled)
campaign.update(time_started: picture.time_posted) if campaign_pictures.first == picture
campaign.update(time_completed: picture.time_posted) if campaign_pictures.last == picture
end
end
Run Code Online (Sandbox Code Playgroud)
错误:
Failure/Error: expect(@campaign.time_started).to eq(@picture1.time_posted)
expected: 2014-07-15 00:00:00.000000000 +0000
got: nil
(compared using ==)
Run Code Online (Sandbox Code Playgroud)
@campaign在进行修改之前,您的变量已被加载,因此您需要在检查新值之前重新加载它。
it 'should update first item in campaign' do
ModelName::PostPicture.send(:update_campaign, @picture)
@campaign.reload
expect(@campaign.time_started).to eq(@picture.time_posted)
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1686 次 |
| 最近记录: |