如何在我的一个模型上测试这些回调?
How do I test these callbacks on 1 of my models?
请注意: 这个问题可能看起来与my other question相似,只是……相似。不相同。我复制了一些信息,因为它与问题相关。话虽这么说,我不想让太多的请求淹没这两个问题,所以我把它们分开了。
我对测试比较陌生,所以如果这个问题看起来很基础,请原谅我。
我正在使用 Rspec、Shoulda、FactoryGirl 和 Faker。我的 class 是 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
FactoryGirl.define do
factory :user do
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
email { Faker::Internet.email }
password { Faker::Internet.password(10) }
password_confirmation { Faker::Internet.password(10) }
bio { Faker::Lorem.paragraph }
invitation_relation { Faker::Lorem.word }
# required if the Devise Confirmable module is used
confirmed_at Time.now
gender 1
end
end
我想真正的问题是,我实际上如何为这些方法本身编写测试,然后如何单独测试回调?
我会正常测试这些方法,甚至不担心它们会被用作回调。这意味着建立一个已知状态,执行您的操作,并为每个状态检查所需的状态。
然后,您可以使用 shoulda-callback-matchers 之类的东西来验证正确的回调是否确实到位,而无需编写大量代码。
或者,您可以简单地建立一个已知状态,运行您的回调方法通过执行触发回调的操作,并检查所需的状态。
请注意: 这个问题可能看起来与my other question相似,只是……相似。不相同。我复制了一些信息,因为它与问题相关。话虽这么说,我不想让太多的请求淹没这两个问题,所以我把它们分开了。
我对测试比较陌生,所以如果这个问题看起来很基础,请原谅我。
我正在使用 Rspec、Shoulda、FactoryGirl 和 Faker。我的 class 是 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
FactoryGirl.define do
factory :user do
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
email { Faker::Internet.email }
password { Faker::Internet.password(10) }
password_confirmation { Faker::Internet.password(10) }
bio { Faker::Lorem.paragraph }
invitation_relation { Faker::Lorem.word }
# required if the Devise Confirmable module is used
confirmed_at Time.now
gender 1
end
end
我想真正的问题是,我实际上如何为这些方法本身编写测试,然后如何单独测试回调?
我会正常测试这些方法,甚至不担心它们会被用作回调。这意味着建立一个已知状态,执行您的操作,并为每个状态检查所需的状态。
然后,您可以使用 shoulda-callback-matchers 之类的东西来验证正确的回调是否确实到位,而无需编写大量代码。
或者,您可以简单地建立一个已知状态,运行您的回调方法通过执行触发回调的操作,并检查所需的状态。