Problem
I am not sure how to test this general pattern in rspec:
When X controller action is triggered, check that the Y attribute is a specific value on object Z
Context
Using public_activity to keep track of when users vote, change vote, write new proposals, etc in an application.
This tracking is ONLY triggered by the controller actions relevant to voting and proposals. Thus, you cannot test activity tracking on the Vote and Proposal model themselves.
You can see the two different methods of activity tracking in this RailsCast http://railscasts.com/episodes/406-public-activity
I can easily test for whether Activity changes when the POST CREATE action for the vote_controller is triggered (see below) but I am stuck with structuring things to test if the most recent Activity (PublicActivity::Activity.last
for example) is recording a custom attribute.
describe 'POST creates' do
context 'current user has no other vote in the proposal tree' do
it 'creates a new vote' do
expect {
post :create, vote: { proposal_id: proposal1.id, comment: "Coz it rocks!"}
}.to change(Vote, :count).by(1)
end
it "increases the Activity count by 1" do
expect {
post :create, vote: { proposal_id: proposal1.id, comment: "Coz it rocks!"}
}.to change(PublicActivity::Activity, :count).by(1)
end
end
In my head, it'd be something like this
it "Activity tracks the id of who needs to be notified of the vote" do
post :create, vote: { proposal_id: proposal1.id, comment: "Coz it rocks!"}
expect { PublicActivity::Activity.last.notify_list[0] }.to eq user2.id
end
Can someone suggest an appropriate pattern/syntax for trying to capture this in an rspec test?
EDIT:
I have also thought about implementing this as tests on the Activity model.
So I would be effectively recreating the logic from the controller This would largely involve setting up at least two users, a proposal (associated with user1), a vote (associated with user2) and then checking that the vote from user2 creates an Activity object that includes user1's id in it's notify_list
@vote.create_activity :voted, owner: user2, notify_list: create_notify_list(@vote, user2)