2

I have the following query:

Trainee.select(:name, :employee_start_date, :exit_date, "count(reviewers.code_review_id) AS reviews_completed")
            .where(user_id: user_ids)
            .joins(:reviewers)
            .where(reviewers: { completion_time: start_date..end_date })
            .group('trainees.user_id').order('reviews_completed DESC')

I am new to rails and rspec and am having a lot of trouble stubbing out this query in my rspec tests. This is what I have so far:

describe '#completed_code_reviews' do
  let(:trainee_service_client) { LeaderboardsService.new('trainee') }
  let(:start_date) { Date.new(2016, 1, 18) - 1.day }
  let(:end_date) { Date.new(2015, 8, 18) + 2.day }
  let(:order) { 'DESC' }

  let(:person_completed_reviews) { 4 }
  let(:select_result) { double('select') }
  let(:where1_result) { double('where1') }
  let(:joins_result) { double('joins') }
  let(:where2_result) { double('where2') }
  let(:group_result) { double('group') }
  let(:trainees_list) { [] }

  context 'a user has no connections' do
    user_ids = []

    it "returns an empty result" do
        allow(Trainee).to receive(:select)
                .with(:name, :devcenter_start_date, :exit_date, 
                    "count(reviewers.code_review_id) AS reviews_completed"
                ).and_return(select_result)

        allow(select_result).to receive(:where).with(user_id: user_ids).and_return(where1_result)

        allow(where1_result).to receive(:joins).with(:reviewers).and_return(joins_result)

        allow(joins_result).to receive(:where)
                .with(reviewers: { completion_time: start_date..end_date.end_of_day })
                .and_return(where2_result)

        allow(where2_result).to receive(:group).with("trainees.user_id").and_return(group_result)

        allow(group_result).to receive(:order).with(person_completed_reviews: order).and_return(trainees_list)


        expect(trainee_service_client.completed_code_reviews(start_date, end_date)).to eq([])
    end
  end
end

Two things:

1) This doesnt actually work.I get: Failure/Error: expect(trainee_service_client.completed_code_reviews(start_date, end_date, [])).to eq(trainee) Double "group" received :order with unexpected arguments expected: ({:person_completed_reviews=>"DESC"}) got: ("reviews_completed DESC")

How do i mock the :order method properly?

2) This setup also seems messy. Is there a more concise way?

1 Answer 1

1

1) You can fix the error with:

allow(group_result).to receive(:order).with("reviews_completed DESC").and_return(trainees_list)

2) This mocking makes no sense to me. Where did you get the idea to do this? Instead, I would create the records you need.

Sign up to request clarification or add additional context in comments.

2 Comments

That did fix the error. I set it up this way because someone showed me an example similar to it. This way you can mock out each step of the query individually and know exactly which point causes an error. That's how my beginner mind understood it at least.
I don't know why you would mock each step of the query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.