I was just starting out with RSpec, and im trying to do something quite trivial, but I couldnt find any good documentation on a good way of doing this. I want to test a sequence of events, say the friendship between two users.
My spec is:
describe User do
describe "friendships" do
describe ".friends?" do
before do
@user1 = FactoryGirl.build(:user)
@user2 = FactoryGirl.build(:user)
end
it "should be false for non friends" do
@user1.friends?(@user2).should be false
@user2.friends?(@user1).should be false
end
it "should be false for requested friendship" do
Friendship.create(:user_id => @user1.id, :friend_id => @user2.id) # 1
@user1.friends?(@user2).should be false
@user2.friends?(@user1).should be false
end
it "should be true for accepted friendship" do
Friendship.for_users(@user1, @user2).update_attribute(:approved, true) # 2
@user1.friends?(@user2).should be true
@user2.friends?(@user1).should be true
end
end
end
end
I am creating a friendship at the line marked # 1, and expect it to be present at line # 2, but I am guessing the database gets flushed in between the two.
Is this the wrong way of testing such a flow of events? What should I be doing? Any pointers would be much appreciated.
I guess you are looking for
context. Something like this:Sorry to say that but you should probably take a look on rspec docs: basic structure (describe/it)