I have the below method
def fetch_starred_posts(page, per)
favorites_post_ids = self.favorites.pluck(:favoriteable_id)
Post.where(id: favorites_post_ids).order("created_at desc").page(page
|| 1).per(per)
end
The TDD for the above code is
describe "#fetch_starred_posts" do
let(:user1) { FactoryGirl.create(:user) }
let(:user2) { FactoryGirl.create(:user) }
let!(:post1) { FactoryGirl.create(:post) }
let!(:post2) { FactoryGirl.create(:post) }
context "when user starred 1 post out of 2 posts " do
before do
user1.favorites.create(favoriteable_id: post1.id,
favoriteable_type: "Post")
user2.favorites.create(favoriteable_id: post2.id,
favoriteable_type: "Post")
end
it "should include post1 in user1's starred posts list, but not post2
" do
expect(user1.fetch_starred_posts(1,2)).to include(post1)
expect(user1.fetch_starred_posts(1,2)).not_to include(post2)
end
it "should include post2 in user2's starred posts list, but not post1
" do
expect(user2.fetch_starred_posts(1,2)).to include(post2)
expect(user2.fetch_starred_posts(1,2)).not_to include(post1)
end
end
context "when pagination is used" do
before do
user1.favorites.create(favoriteable_id: post1.id,
favoriteable_type: "Post")
user1.favorites.create(favoriteable_id: post2.id,
favoriteable_type: "Post")
end
it "should return only one starred post in first page" do
page, per = 1, 1
starred_posts = user1.fetch_starred_posts(page, per)
expect(starred_posts.count).to eq(1)
expect(starred_posts).to include(post2)
end
it "should return only one starred post in first page" do
page, per = 2, 1
starred_posts = user1.fetch_starred_posts(page, per)
expect(starred_posts.count).to eq(1)
expect(starred_posts).to include(post1)
end
end
end
The Rspec is running perfectly. But in the *pagination* part the *before*
block running for each example, whereas it is not needed. Can I do execute
the code once for both *it* examples not twice ?
--
You received this message because you are subscribed to the Google Groups
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rspec/5836ce31-8c2c-4712-b47c-95ba28c08708%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.