0

我想测试一个特定的图标是否会显示在一个用户的视图中,该用户的连续性超过 X 天。所以我需要存根模型的一个streak方法。但User我希望它只为基于它的特定用户存根方法uid。测试代码如下。

test "should display an icon for users with streak longer than 7 days" do
    node = node(:one)
    User.any_instance.stubs(:streak).returns([8,10])
    get :show, 
        author: node.author.username, 
        date: node.created_at.strftime("%m-%d-%Y"),
        id: node.title.parameterize
    assert_select ".fa-fire", 1
end

返回值是一个数组,数组中的第一个值是连续的天数,第二个值是该连续的帖子数。

该行User.any_instance.stubs(:streak).returns([8,10])存根类的任何实例User。我怎样才能存根它以便它只存根那些实例:uid => 1

4

1 回答 1

0

听起来您应该对特定实例进行存根,而不是对类本身进行存根。

User.where.not(uid: 1).each do |user|
  user.stubs(:streak).returns([8,10])
end

或者也许(我不能肯定地说没有更多的上下文),你可以通过这样做来优化它:

node.author.stubs(:streak).returns([8,10])
于 2016-05-19T12:30:54.900 回答