Skip to content

Commit b5d67c7

Browse files
committed
Add food controller tests
1 parent b7f9d57 commit b5d67c7

File tree

17 files changed

+122
-34
lines changed

17 files changed

+122
-34
lines changed

app/assets/javascripts/custom.js

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ $(document).ready(function(){
106106
" <p>" + comment.comment + "</p>" +
107107
" </div>"
108108
);
109+
$("#comment").val('');
109110
})
110111
});
111112

app/controllers/carts_controller.rb

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ def index
44
@ordered_foods = {}
55
@total = 0
66
@pickup_time = 0
7-
@prep_total = 0
87
@cart_items.each do |food_id, qty, prep_time|
98
food = Food.find_by_id(food_id)
109
prep_time = line_prep_total(qty, food.prep_time).to_i
@@ -29,8 +28,6 @@ def check_food_status(food, qty, food_id, prep_time)
2928
@ordered_foods.delete(food_id)
3029
else
3130
@total += (food.price * qty)
32-
# add_extra_time(prep_time)
33-
@prep_total += prep_time
3431
end
3532
end
3633

@@ -39,8 +36,9 @@ def line_prep_total(qty, prep_time)
3936
end
4037

4138
def add_extra_time(pick_up_time)
42-
if (Order.first.Status != "Delivered") || (Order.first.Status == "Cancelled")
43-
pick_up_time + 4
39+
if (Order.first.Status != "Delivered") ||
40+
(Order.first.Status == "Cancelled")
41+
pick_up_time + 4
4442
else
4543
pick_up_time
4644
end

app/views/foods/new.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@
4343
<div class="determinate" style="width: 70%"></div>
4444
</div>
4545
</div>
46-
<%= f.submit "Sign up", :class => "btn waves-effect waves-light" %>
46+
<%= f.submit "Add food", :class => "btn waves-effect waves-light" %>
4747
<% end %>
4848
</div>

dump.rdb

302 Bytes
Binary file not shown.
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

spec/features/checkouts_spec.rb

+14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
require 'rails_helper'
22

33
RSpec.feature "Checkouts", type: :feature do
4+
5+
before(:each) do
6+
O1 = Order.create(Status: "Completed", total: 23600,
7+
vat: 2100, delivery_cost: 500, user_id: 6,
8+
created_at: "2015-10-20 15:49:28", updated_at: "2015-10-20 15:49:28",
9+
transaction_id: "1VX775486T314881H", invoice: "", pickup_time: 26)
10+
11+
O2 = Order.create(Status: "Completed", total: 23600,
12+
vat: 2100, delivery_cost: 500, user_id: 6,
13+
created_at: "2015-10-20 15:49:28", updated_at: "2015-10-20 15:49:28",
14+
transaction_id: "1VX775486T314881H", invoice: "", pickup_time: 26)
15+
end
16+
417
describe "User can check out from Carts page" do
518
scenario "user clicks checkout button on carts page" do
619
Food.create(name: "coconut rice", description: "tasty coconut rice cooked with shrimps", price: 4000)
20+
Order.create(Status: "Completed", total: 23600, vat: 2100, delivery_cost: 500, user_id: 6, created_at: "2015-10-20 15:49:28", updated_at: "2015-10-20 15:49:28", transaction_id: "1VX775486T314881H", invoice: "", pickup_time: 26)
721
visit foods_path
822
click_button "Add to cart"
923

spec/features/foods_spec.rb

+65-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,73 @@
11
require "rails_helper"
22
RSpec.feature "Foods", type: :feature do
3-
scenario "the food index page shows all the food" do
4-
Food.create(
3+
before(:each) do
4+
@user = User.create(first_name: "Toyosi",
5+
last_name: "Famakinde",
6+
7+
password: "jeffrules",
8+
password_confirmation: "jeffrules",
9+
role: "admin")
10+
Category.create(title: "Entree", description: "first meal")
11+
@F1 = Food.create(
512
name: "Spaghetti",
613
description: "Best spaghetti ever",
7-
price: 1500.00
8-
)
9-
Food.create(
10-
name: "Jollof rice",
11-
description: "Hot spicy jollof ever",
12-
price: 1600.00
13-
)
14+
price: 1500.00 )
15+
visit root_path
16+
17+
click_link('Log in')
18+
expect(current_path).to eq(login_path)
19+
expect(page).to have_selector("h3", text: "Log in")
20+
fill_in "session_email", with: '[email protected]'
21+
fill_in "session_password", with: 'jeffrules'
22+
click_button('Log in')
23+
end
24+
25+
scenario "the food index page shows all the food" do
1426
visit foods_path
15-
expect(page).to have_content("Jollof rice")
1627
expect(page).to have_content("Spaghetti")
1728
end
29+
30+
scenario "admin should be able to add new food" do
31+
expect(current_path).to eq("/admin")
32+
33+
visit("foods/new")
34+
expect(page).to have_content("Create new food")
35+
page.fill_in "food_name", :with => "Pancake n Syrup"
36+
page.fill_in "food_description", :with => "Juicy syrup on fresh home-baked pancakes"
37+
page.fill_in "food_price", :with => 100
38+
page.fill_in "food_prep_time", :with => 12
39+
select('Entree', from: 'food_category_id')
40+
page.attach_file('food[food_image]', Rails.root + 'app/assets/images/chicken.jpg')
41+
click_button "Add food"
42+
expect(current_path).to eq admin_foods_path
43+
44+
expect(page).to have_content("Pancake n Syrup")
45+
46+
end
47+
48+
scenario "admin should edit food" do
49+
visit(admin_foods_path)
50+
click_link("Spaghetti")
51+
expect(current_path).to eq edit_food_path(@F1.id)
52+
53+
page.fill_in "food_name", :with => "Pancake and meatballs"
54+
page.attach_file('food[food_image]', Rails.root + 'app/assets/images/chicken.jpg')
55+
56+
click_button "Add food"
57+
expect(current_path).to eq admin_foods_path
58+
59+
expect(page).to have_content("Pancake and meatballs")
60+
end
61+
62+
# scenario "admin should edit food status" do
63+
# visit(admin_foods_path)
64+
# uncheck('A Checkbox')
65+
# end
66+
67+
1868
end
69+
70+
71+
72+
73+

spec/features/user_add_items_to_carts_spec.rb

+28-9
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,46 @@
22

33
RSpec.feature "UserAddItemsToCarts", type: :feature do
44
before(:each) do
5-
Food.create(
5+
F1 = Food.create(
66
name: "Spagetti bolognese",
77
description: "Warm yummy spagetti to increase your life span!",
8-
price: 1500
9-
)
10-
end
8+
price: 1500,
9+
prep_time: 12)
10+
11+
O1 = Order.create(Status: "Completed", total: 23600,
12+
vat: 2100, delivery_cost: 500, user_id: 6,
13+
created_at: "2015-10-20 15:49:28", updated_at: "2015-10-20 15:49:28",
14+
transaction_id: "1VX775486T314881H", invoice: "", pickup_time: 26)
15+
end
1116

1217
describe "add items to cart" do
1318
scenario "user adds first item to cart" do
1419
visit foods_path
1520
expect(page).to have_css("#cart", text: "0")
1621
click_button "Add to cart"
1722
expect(page).to have_css("#cart", text: "1")
18-
end
19-
20-
scenario "user chooses to add the same item again" do
21-
visit foods_path
2223
3.times do
2324
click_button "Add to cart"
2425
end
25-
expect(page).to have_css("#cart", text: "3")
26+
expect(page).to have_css("#cart", text: "4")
27+
28+
visit carts_path
29+
expect(page).to have_content("#{F1.name}")
30+
expect(page).to have_content("#{F1.price}")
31+
expect(page).to have_css(".line-total", text: "6000")
32+
33+
select('4', from: 'Select Box')
34+
35+
2636
end
2737
end
38+
39+
# scenario "user chooses to add the same item again" do
40+
# visit foods_path
41+
# 3.times do
42+
# click_button "Add to cart"
43+
# end
44+
# expect(page).to have_css("#cart", text: "3")
45+
# end
46+
# end
2847
end

spec/models/cart_spec.rb

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,43 @@
11
# require "rails_helper"
2-
#
2+
33
# RSpec.describe Cart, type: :model do
44
# before(:each) do
55
# food_id = 1
66
# @cart = Cart.new(session[:cart])
7+
# require "pry-nav"; binding.pry
78
# @cart.increment(food_id)
8-
# # session[:cart] = @cart.cart_data
9+
# session[:cart] = @cart.cart_data
910
# end
10-
#
11+
1112
# describe "Place first item in cart" do
1213
# it "should create a new cart" do
1314
# expect(@cart).to exist
1415
# expect(session[:cart]).to exist
1516
# end
16-
#
17+
1718
# it "should make new item in cart equal to 1" do
1819
# expect(@cart[:food_id]).to eq 1
1920
# end
2021
# end
21-
#
22+
2223
# describe "#update_items" do
2324
# it "should increment the qty of items in cart by 1" do
2425
# qty = 3
2526
# @cart.update(qty)
2627
# expect(@cart[1]).to eq 4
2728
# end
2829
# end
29-
#
30-
#
30+
31+
3132
# describe "#destroy" do
3233
# it "should empty the cart" do
3334
# @cart.destroy!
3435
# expect(@cart).to eq nil
3536
# end
36-
#
37+
3738
# it "notifies the user the cart has been deleted" do
3839
# expect(flash[:notice]).to be_present
3940
# end
4041
# end
41-
#
42+
4243
# end

0 commit comments

Comments
 (0)