1
describe "Team::Membership" do
let(:team) { Team.last }
let(:user) { User.last }
let(:membership) {user.apply_for(team)}
it "can be rejected" do
assert_difference "team.denied_applications.reload.count", +1 do
assert_difference "team.applications.reload.count", -1 do
assert_difference "team.members.reload.count", 0 do
membership.reject!
end
end
end
membership.state.must_equal "denied"
end
如果申請被拒絕,則範圍denied_applications
增加1,applications
爲-1,members
根本不會改變。這是一個很好的書面測試還是很難看?
# team.rb
has_many :memberships, dependent: :destroy
has_many :members, -> { where(team_memberships: {state: :confirmed}) }, through: :memberships, source: :user
has_many :ex_members, -> { where(team_memberships: {state: :left}) }, through: :memberships, source: :user
has_many :applications, -> { where(state: :pending) }, class_name: "Membership"
has_many :denied_applications, -> { where(state: :denied) }, class_name: "Membership"
所以我的問題是更可能:是一個很好的軟件寫測試或者你只是寫
team.denied_applications.reload.count.must_equal(1)
team.applications.reload.count.must_equal(0)
team.members.reload.count.must_equal(0)
我確實使用factory_girl'Team.last'indeed來自seeds.rb。整個項目很大,我們需要種子進行測試。用戶創建一個團隊。團隊創建各種項目,該團隊的一些用戶可以對項目進行一些操作。我覺得用factorygirl運行所有這些東西並不現實,因爲obv。團隊首先需要批准,項目需要批准。所以只需運行1個測試小測試,然後我需要運行至少20行的ruby代碼,只是爲了測試場景 –
我絕對看到你來自哪裏。也許'factory_girl'特徵可以幫助你,所以你可以有某種'創建(:團隊,:批准)'左右? – jfornoff