如何树立共享榜样?

反k

我只是在学习用红宝石和Rails编写规范。因此,我确实有两个类似的模型,投票时它们的行为类似:问与答。因此,我尝试不重复代码,并为这两个示例编写共享示例。

RSpec.shared_examples_for 'User Votable' do

  let!(:user){ create :user } 
  let!(:sample_user){ create :user }   
  let!(:vote){ create :vote, user: user, votable: votable, vote_field: 1}   

  it 'user different from resource user is accapteble' do        
    expect(votable.user_voted?(sample_user)).to be_falsy
  end   

  it 'user similar to resource user is accapteble' do

    expect(votable.user_voted?(user)).to be_truthy
  end

end

和测试本身

describe 'user_voted?' do 
  def votable
    subject{ build(:question)}
  end
  it_behaves_like 'User Votable'
end

本规范中的最后一个失败(我认为是由于主题问题-当我进行投票时它不会改变),因此,如果我能够管理并理解如何正确地进行操作,我将非常高兴。非常感谢您的任何建议

另外,当我尝试使用模拟程序时,它抱怨没有主键

allow(:question){create :question}

Failures:

1)问题是user_voted吗?行为类似于用户Votable用户,类似于资源用户失败/错误:Expect(votable.user_voted?(user))。to be_truthy

   expected: truthy value
        got: false
 Shared Example Group: "User Votable" called from ./spec/models/question_spec.rb:23
克里斯

除了使用votable方法之外,您还可以subject这样设置

it_behaves_like 'User Votable' do
  subject { build(:question) }
end

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章