On Fri, Jun 13, 2014 at 9:22 AM, Arup Rakshit <[email protected]> wrote:

> 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 ?
>

As I understand it:

before(:all) runs the block one time before all of the examples are run.
before(:each) runs the block one time before each of your specs in the file

http://stackoverflow.com/questions/16617052/rails-rspec-before-all-vs-before-each


@robpark

>

-- 
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/CANkqiS3WoBXSJPbo7x_d4v0iceG%2B3wBq1xYWSsLy-jSBsyQ-6w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to