首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails模型规范通过,Sidekiq规范失败

Rails模型规范通过,Sidekiq规范失败
EN

Stack Overflow用户
提问于 2014-10-09 09:29:16
回答 1查看 242关注 0票数 0

我正在构建一个运行投票的Rails (4.1.0)应用程序。每个投票都有与n席位的n比赛。以下是我的模型:

代码语言:javascript
复制
class Matchup < ActiveRecord::Base
  has_many :seats, dependent: :destroy

  def winning_seat
    seats.sort { |a,b| a.number_of_votes <=> b.number_of_votes }.last
  end
end

class Seat < ActiveRecord::Base
  belongs_to :matchup

  validates :matchup, presence: true
  validates :number_of_votes, presence: true

  def declare_as_winner
    self.is_winner = true
    self.save
  end

end

我的比赛规格和座位通行证都没有问题。在投票结束时,我需要显示获胜者。我正在使用Sidekiq工作人员来处理投票的结束。它可以做很多事情,但下面是有问题的代码:

代码语言:javascript
复制
class EndOfPollWorker
  include Sidekiq::Worker

def perform(poll_id)
  poll = Poll.where(:id poll_id)
  poll.matchups.each do |matchup|
    # grab the winning seat
    winning_seat = matchup.winning_seat
    # declare it as a winner
    winning_seat.declare_as_winner
  end
end
end

此工人的规格不合格:

代码语言:javascript
复制
require 'rails_helper'

describe 'EndOfPollWorker' do
  before do
    #this simple creates a matchup for each poll question and seat for every entry in the matchup
    @poll = Poll.build_poll  
  end

  context 'when the poll ends' do
    before do
      @winners = @poll.matchups.map { |matchup| matchup.seats.first }
      @losers = @poll.matchups.map { |matchup| matchup.seats.last }
      @winners.each do |seat|
        seat.number_of_votes = 1
      end

      @poll.save!

      @job = EndOfPollWorker.new
    end

    it 'it updates the winner of each matchup' do
      @job.perform(@poll.id)
      @winners.each do |seat|
        expect(seat.is_winner?).to be(true)
      end
    end

    it 'it does not update the loser of each matchup' do
      @job.perform(@poll.id)
      @losers.each do |seat|
        expect(seat.is_winner?).to be(false)
      end
    end

  end

  end
end
end

当我运行这个规范时,我得到:

代码语言:javascript
复制
EndOfPollWorker when poll ends it updates the winner of each matchup
     Failure/Error: expect(seat.is_winner?).to be(true)

       expected true
            got false

我的座椅和对抗赛模型的规格都通过了。我删掉了很多测试代码,所以请原谅任何不匹配的标签,假设这不是问题所在!

此外,当工作线程实际在开发模式下运行时,seats.is_winner属性实际上不会更新。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2014-10-09 12:17:04

Sidekiq和你的问题无关。您直接调用perform,所以问题出在rspec和activerecord上。例如,将代码从perform方法中提取出来并将其直接放入规范中,它应该仍然会失败。

我怀疑这些实例已经过时了,需要从数据库中重新加载这些实例,以获取在#perform中所做的更改。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26268950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档