如何在我的一个模型上测试这些回调?

时间:2015-04-24 23:45:44

标签: ruby-on-rails ruby-on-rails-4 rspec factory-bot shoulda

请注意: 此问题可能与my other question类似,只是......类似。不一样。我复制了一些信息,因为它与问题有关。话虽这么说,我并不想用太多的请求来压倒任何一个问题,所以我将它们分开了。

我对测试比较陌生,所以如果这个问题看起来很基本,请原谅我。

我正在使用Rspec,Shoulda,FactoryGirl和Faker。我的班级是jQuery.fn.jquery

这些是架构&我的User模型的关联:

User

我不确定测试的方法(回调)是:

# == Schema Information
#
# Table name: users
#
#  id                     :integer          not null, primary key
#  email                  :string(255)      default(""), not null
#  encrypted_password     :string(255)      default("")
#  reset_password_token   :string(255)
#  reset_password_sent_at :datetime
#  remember_created_at    :datetime
#  sign_in_count          :integer          default(0), not null
#  current_sign_in_at     :datetime
#  last_sign_in_at        :datetime
#  current_sign_in_ip     :string(255)
#  last_sign_in_ip        :string(255)
#  created_at             :datetime
#  updated_at             :datetime
#  first_name             :string(255)
#  confirmation_token     :string(255)
#  confirmed_at           :datetime
#  confirmation_sent_at   :datetime
#  unconfirmed_email      :string(255)
#  invitation_relation    :string(255)
#  avatar                 :string(255)
#  invitation_token       :string(255)
#  invitation_created_at  :datetime
#  invitation_sent_at     :datetime
#  invitation_accepted_at :datetime
#  invitation_limit       :integer
#  invited_by_id          :integer
#  invited_by_type        :string(255)
#  invitations_count      :integer          default(0)
#  bio                    :text
#  last_name              :string(255)
#  gender                 :integer          default(0)
#

  has_one :family_tree, dependent: :destroy
  has_many :memberships, dependent: :destroy
  has_many :nodes, dependent: :destroy
  has_many :comments, dependent: :destroy

  enum gender: { male: 0, female: 1 }

  after_create :create_family_tree
  after_destroy :remove_all_memberships
  after_invitation_accepted :send_emails

到目前为止,这是我的 private def create_family_tree family_tree = self.build_family_tree(name: "#{name}'s Family Tree") family_tree.save! end def remove_all_memberships memberships = Membership.includes(:user).where(user_id: self.id) memberships.destroy_all end def send_emails invited_user = self inviter_user = self.invited_by welcome_message = InviteAcceptance.welcome_to_invited_user_after_signing_up(invited_user, inviter_user) welcome_message.deliver notification_message = InviteAcceptance.notification_that_invited_user_signed_up(inviter_user, invited_user) notification_message.deliver end ,它只测试了关联和性别值,两者都很好:

user_spec.rb

这是我的require 'rails_helper' describe User do it { should have_one(:family_tree) } it { should have_many(:memberships) } it { should have_many(:nodes) } it { should have_many(:comments) } it "should return gender from an enum value, where 0 = male, and 1 = female" do expect(build(:user).gender).to eq "female" end end

spec/factories/users.rb

我想真正的问题是,我如何为这些方法本身编写测试,然后如何单独测试回调?

1 个答案:

答案 0 :(得分:2)

我会正常测试这些方法,甚至不用担心它们会被用作回调。这意味着建立一个已知状态,执行操作,并为每个状态检查所需的状态。

然后,您可以使用shoulda-callback-matchers之类的内容来验证是否实际存在正确的回调,而无需编写大量代码。

或者,您可以简单地建立一个已知状态,通过执行触发回调的操作来运行您的回调方法,并检查所需的状态。