如何正确测试使用未读宝石的模型(在rspec中)?

时间:2012-10-11 19:43:36

标签: ruby-on-rails-3 testing gem rspec2 factory-bot

我有一个应用程序正在使用unread gem将某些模型标记为已读或未读用户。宝石是甜美的,在应用程序中完美无瑕地工作。不幸的是,当我处理一些涉及act_as_readable我不能的模型的Rspec测试时,我遇到了一些奇怪的错误。对于我的生活,我想知道下一步该怎么做。我希望令人惊叹的StackOverflow社区可以帮助我。以下是必要的细节:

模型方法

def unread_subscriptions
    reviews = Array.new
    subscriptions = Subscription.where(user_id: self.id)

    unless subscriptions.empty?
        subscriptions.each do |subscription|
            subscription_reviews = Review.where(user_id: subscription.subscription_user_id)

            unless subscription_reviews.empty?
                subscription_reviews.each do |subscription_review|
                    if subscription_review.unread?(self)
                        reviews << subscription_review
                    end
                end
            end
        end
    end

    reviews.sort_by(&:created_at).reverse
end

这段代码有一些明显的地方可以改进或重新考虑因素,这正是我为它编写测试的原因所以请让我们继续把讨论集中在手头的错误上。

Rspec测试块

describe "#unread_subscriptions", focus: true do
    context "when subscriptions and subscription reviews are not empty" do
        let(:subscription_user) { FactoryGirl.create(:user) }
        let(:reviews) { FactoryGirl.create_list(Review, 3, user: subscription_user) }

        before(:each) {
            subscription_double = double(Subscription)
            subscription_double.stub(:where)
                                .with(user_id: user.id)
                                .and_return(
                                    FactoryGirl.create(:subscription, 
                                        user_id: user.id, 
                                        subscription_user_id: subscription_user.id))
            review_double = double(Review)
            review_double.stub(:unread_by).with(user).and_return(reviews)
            review_double.stub(:unread?).with(user).and_return(true)
            review_double.stub(:where).with(user_id: subscription_user.id).and_return(reviews)
        }

        its(:unread_subscriptions) { should be_instance_of(Array) }
        its(:unread_subscriptions) { should_not be_empty }
    end
end

以上所有代码似乎都能正常运行。 subscription_user由FactoryGirl正确创建并具有ID。 FactoryGirl reviews生成的create_list数组是合法的,并且user_id已正确填充。从上面的代码中可以看出,我已经尝试在unread类上删除Review gem的方法,但它们似乎没有正常工作。

审核工厂

FactoryGirl.define do
    sequence :created_at do |n|
        n.minutes.ago
    end
    factory :review do
        user
        bar
        body "MyText"
        rating 3
        created_at
    end
end

我使用rspec spec/models/user_spec.rb -f d运行这些测试并获得以下输出:

Run options: include {:focus=>true}

User
  #unread_subscriptions
    when subscriptions and subscription reviews are not empty
      unread_subscriptions
        example at ./spec/models/user_spec.rb:231 (FAILED - 1)
      unread_subscriptions
        example at ./spec/models/user_spec.rb:232 (FAILED - 2)

Failures:

  1) User#unread_subscriptions when subscriptions and subscription reviews are not empty unread_subscriptions 
     Failure/Error: its(:unread_subscriptions) { should be_instance_of(Array) }
     ActiveRecord::StatementInvalid:
       Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND read_marks.timestamp     >= reviews.created_at WHERE `reviews`.`id` = 1 AND ' at line 4: SELECT  1 FROM `reviews` LEFT JOIN read_marks ON read_marks.readable_type  = 'Review'
                                                         AND read_marks.readable_id    = reviews.id
                                                         AND read_marks.user_id        = 
                                                         AND read_marks.timestamp     >= reviews.created_at WHERE `reviews`.`id` = 1 AND (read_marks.id IS NULL) ORDER BY created_at DESC LIMIT 1
     # ./app/models/user.rb:217:in `block (2 levels) in unread_subscriptions'
     # ./app/models/user.rb:216:in `block in unread_subscriptions'
     # ./app/models/user.rb:212:in `unread_subscriptions'
     # ./spec/models/user_spec.rb:231:in `block (4 levels) in <top (required)>'

  2) User#unread_subscriptions when subscriptions and subscription reviews are not empty unread_subscriptions 
     Failure/Error: its(:unread_subscriptions) { should_not be_empty }
     ActiveRecord::StatementInvalid:
       Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND read_marks.timestamp     >= reviews.created_at WHERE `reviews`.`id` = 4 AND ' at line 4: SELECT  1 FROM `reviews` LEFT JOIN read_marks ON read_marks.readable_type  = 'Review'
                                                         AND read_marks.readable_id    = reviews.id
                                                         AND read_marks.user_id        = 
                                                         AND read_marks.timestamp     >= reviews.created_at WHERE `reviews`.`id` = 4 AND (read_marks.id IS NULL) ORDER BY created_at DESC LIMIT 1
     # ./app/models/user.rb:217:in `block (2 levels) in unread_subscriptions'
     # ./app/models/user.rb:216:in `block in unread_subscriptions'
     # ./app/models/user.rb:212:in `unread_subscriptions'
     # ./spec/models/user_spec.rb:232:in `block (4 levels) in <top (required)>'

Finished in 8.88 seconds
2 examples, 2 failures

Failed examples:

rspec ./spec/models/user_spec.rb:231 # User#unread_subscriptions when subscriptions and subscription reviews are not empty unread_subscriptions 
rspec ./spec/models/user_spec.rb:232 # User#unread_subscriptions when subscriptions and subscription reviews are not empty unread_subscriptions

由于某种原因,readmarks查询未获得审核的用户ID(即使审核与用户相关联)。我对这里发生的事情绝对不知所措,真正需要某人的天才让我指出正确的方向。感谢任何人都能提供的帮助。为了完整起见,这里有一些关于我的环境的事实:

  • 操作系统:Mac OSX 10.7.5
  • Ruby版本:ruby 1.9.3p194(2012-04-20修订版35410)[x86_64-darwin11.4.0]
  • Rails版本:Rails 3.2.3
  • RVM版本:rvm 1.15.8(稳定)
  • RSpec版本:2.11.1
  • 未读的gem版本:0.1.1
  • factory_girl_rails版本:4.1.0

额外:我还在使用this thread中有关wait方法的共享宏的建议。它没有解决错误。

1 个答案:

答案 0 :(得分:0)

我没有详细检查你的代码,但在我看来你使用带有无效参数的范围“unread_by”。它必须是持久用户(具有ID)。

我刚刚给gem添加了一个改进的param检查并且碰到了0.1.2,所以请用最新版本测试你的代码。