master
1require "rails_helper"
2
3describe WorkoutsController do
4 let(:user) { create(:user) }
5
6 before :each do
7 http_login(user)
8 end
9
10 describe "#index" do
11 include_context "stronglifts_program"
12 let!(:workout_a) { create(:workout, user: user, routine: routine_a, occurred_at: 1.week.ago) }
13 let!(:workout_b) { create(:workout, user: user, routine: routine_b, occurred_at: 1.day.ago) }
14
15 it "loads all my workouts" do
16 get :index
17 expect(assigns(:workouts)).to match_array([workout_a, workout_b])
18 end
19
20 it "loads all works since a given time" do
21 get :index, params: { since: 2.days.to_i }
22 expect(assigns(:workouts)).to match_array([workout_b])
23 end
24 end
25
26 describe "#new" do
27 include_context "stronglifts_program"
28
29 it "loads the next routine in the program" do
30 create(:workout, user: user, routine: routine_a)
31
32 get :new
33
34 expect(assigns(:routine)).to eql(routine_b)
35 end
36
37 it "loads the first routine in the program" do
38 get :new
39
40 expect(assigns(:routine)).to eql(routine_a)
41 end
42 end
43
44 describe "#create" do
45 include_context "stronglifts_program"
46 let(:body_weight) { rand(250.0).lbs }
47
48 it "creates a new workout" do
49 post :create, params: {
50 workout: {
51 routine_id: routine_b.id,
52 body_weight: body_weight
53 }
54 }
55 expect(user.reload.workouts.count).to eql(1)
56 expect(user.last_routine).to eql(routine_b)
57 expect(user.workouts.last.body_weight).to eql(body_weight)
58 expect(response).to redirect_to(edit_workout_path(user.workouts.last))
59 end
60
61 it "creates the workout with the selected exercises" do
62 post :create, params: {
63 workout: {
64 routine_id: routine_b.id,
65 body_weight: body_weight,
66 exercise_sets_attributes: [{
67 exercise_id: squat.id,
68 target_repetitions: 5,
69 target_weight: 200,
70 type: "WorkSet",
71 }]
72 }
73 }
74
75 expect(user.reload.workouts.count).to eql(1)
76 expect(user.last_routine).to eql(routine_b)
77 workout = user.workouts.last
78 expect(workout.body_weight).to eql(body_weight)
79 expect(workout.routine).to eql(routine_b)
80 expect(workout.sets.count).to eql(1)
81 expect(response).to redirect_to(edit_workout_path(user.workouts.last))
82 end
83 end
84
85 describe "#edit" do
86 let(:workout) { create(:workout, user: user) }
87
88 it "loads the workouts" do
89 get :edit, params: { id: workout.id }
90 expect(assigns(:workout)).to eql(workout)
91 end
92 end
93end