# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
-one:
- title: MyString
- description: MyText
- image_url:
+ruby_book:
+ title: Programming Ruby
+ description: Dummy description
+ price: 1234
+ image_url: ruby.png
-two:
- title: MyString
- description: MyText
- image_url:
+rails_book:
+ title: Agile Web Development with Rails
+ description: Dummy description
+ price: 2345
+ image_url: rails.png
+
\ No newline at end of file
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
-one:
- name: MyString
- hashed_password: MyString
- salt: MyString
-
-two:
- name: MyString
- hashed_password: MyString
- salt: MyString
+<% SALT = "NaCl" unless defined?(SALT) %>
+dave:
+ name: dave
+ salt: <%= SALT %>
+ hashed_password: <%= User.encrypted_password('secret' , SALT) %>
\ No newline at end of file
require 'test_helper'
class AdminControllerTest < ActionController::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
+
+ fixtures :users
+
+ test "index without user" do
+ get :index
+ assert_redirected_to :action => "login"
+ assert_equal "Please log in", flash[:notice]
+ end
+
+ test "index with user" do
+ get :index, {}, { :user_id => users(:dave).id }
+ assert_response :success
+ assert_template "index"
+ end
+
+ test "login" do
+ dave = users(:dave)
+ post :login, :name => dave.name, :password => 'secret'
+ assert_redirected_to :action => "index"
+ assert_equal dave.id, session[:user_id]
+ end
+
+ test "bad password" do
+ dave = users(:dave)
+ post :login, :name => dave.name, :password => 'wrong'
+ assert_template "login"
end
+
end
--- /dev/null
+require 'test_helper'
+
+class UserStoriesTest < ActionController::IntegrationTest
+ fixtures :products
+
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+
+ # A user goes to the index page. They select a product, adding it to their
+ # cart, and check out, filling in their details on the checkout form. When
+ # they submit, an order is created containing their information, along with a
+ # single line item corresponding to the product they added to their cart.
+
+ test "buying a product" do
+ LineItem.delete_all
+ Order.delete_all
+ ruby_book = products(:ruby_book)
+
+ get "/store/index"
+ assert_response :success
+ assert_template "index"
+
+ xml_http_request :put, "/store/add_to_cart" , :id => ruby_book.id
+ assert_response :success
+
+ cart = session[:cart]
+ assert_equal 1, cart.items.size
+ assert_equal ruby_book, cart.items[0].product
+
+ post "/store/checkout"
+ assert_response :success
+ assert_template "checkout"
+
+ post_via_redirect "/store/save_order" ,
+ :order => { :name => "Dave Thomas" ,
+ :address => "123 The Street" ,
+ :email => "dave@pragprog.com" ,
+ :pay_type => "cheque" }
+ assert_response :success
+ assert_template "index"
+# assert_template "store/checkout"
+
+ assert_equal 0, session[:cart].items.size
+ orders = Order.find(:all)
+ assert_equal 1, orders.size
+
+ order = orders[0]
+ assert_equal "Dave Thomas", order.name
+ assert_equal "123 The Street", order.address
+ assert_equal "dave@pragprog.com", order.email
+ assert_equal "cheque", order.pay_type
+
+ assert_equal 1, order.line_items.size
+ line_item = order.line_items[0]
+ assert_equal ruby_book, line_item.product
+ end
+end
--- /dev/null
+require 'test_helper'
+
+class CartTest < ActiveSupport::TestCase
+
+ fixtures :products
+
+ def setup
+ @cart = Cart.new
+ @rails = products(:rails_book)
+ @ruby = products(:ruby_book)
+ end
+
+ def test_add_unique_products
+ @cart.add_product @rails
+ @cart.add_product @ruby
+ assert_equal 2, @cart.items.size
+ assert_equal @rails.price + @ruby.price, @cart.total_price
+ end
+
+ def test_add_duplicate_product
+ @cart.add_product @rails
+ @cart.add_product @rails
+ assert_equal 2*@rails.price, @cart.total_price
+ assert_equal 1, @cart.items.size
+ assert_equal 2, @cart.items[0].quantity
+ end
+
+end
\ No newline at end of file
require 'test_helper'
class ProductTest < ActiveSupport::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
+
+ fixtures :products
+
+ test "Invalid with empty attributes" do
+ product = Product.new
+ assert !product.valid?
+ assert product.errors.invalid?(:title)
+ assert product.errors.invalid?(:description)
+ assert product.errors.invalid?(:price)
+ assert product.errors.invalid?(:image_url)
end
+
+ test "positive price" do
+ product = Product.new(:title => "My Book Title" ,
+ :description => "yyy" ,
+ :image_url => "zzz.jpg" )
+ product.price = -1
+ assert !product.valid?
+ assert_equal "should be at least 0.01" , product.errors.on(:price)
+ product.price = 0
+ assert !product.valid?
+ assert_equal "should be at least 0.01" , product.errors.on(:price)
+ product.price = 1
+ assert product.valid?
+ end
+
+ test "image url" do
+ ok = %w{ fred.gif fred.jpg fred.png FRED.JPG FRED.Jpg http://a.b.c/x/y/z/fred.gif }
+ bad = %w{ fred.doc fred.gif/more fred.gif.more }
+ ok.each do |name|
+ product = Product.new(:title => "My Book Title" ,
+ :description => "yyy" ,
+ :price => 1,
+ :image_url => name)
+ assert product.valid?, product.errors.full_messages
+ end
+ bad.each do |name|
+ product = Product.new(:title => "My Book Title" ,
+ :description => "yyy" ,
+ :price => 1,
+ :image_url => name)
+ assert !product.valid?, "saving #{name}"
+ end
+ end
+
+ test "unique title" do
+ product = Product.new(:title => products(:ruby_book).title,
+ :description => "yyy" ,
+ :price => 1,
+ :image_url => "fred.gif" )
+ assert !product.save
+ assert_equal I18n.translate('activerecord.errors.messages.taken'), product.errors.on(:title)
+ end
+
end
--- /dev/null
+Have a lovely picture, from me. Enjoy!
\ No newline at end of file
--- /dev/null
+let's go!
\ No newline at end of file
--- /dev/null
+body: <%= @body %>
+bar: <%= @bar %>
\ No newline at end of file
--- /dev/null
+%p Hello there,
+
+%p
+ Mr.
+ = @recipient
+ from haml
\ No newline at end of file
--- /dev/null
+%p Hello there,
+
+%p
+ Mr.
+ = @recipient
+ from haml
\ No newline at end of file
--- /dev/null
+Ignored when searching for implicitly multipart parts.
--- /dev/null
+Ignored when searching for implicitly multipart parts.
--- /dev/null
+<html>
+ <body>
+ HTML formatted message to <strong><%= @recipient %></strong>.
+ </body>
+</html>
+<html>
+ <body>
+ HTML formatted message to <strong><%= @recipient %></strong>.
+ </body>
+</html>
--- /dev/null
+Plain text to <%= @recipient %>.
+Plain text to <%= @recipient %>.
--- /dev/null
+yaml to: <%= @recipient %>
\ No newline at end of file
--- /dev/null
+Hey Ho, <%= render :partial => "subtemplate" %>
\ No newline at end of file
--- /dev/null
+xml.instruct!
+xml.test
\ No newline at end of file
--- /dev/null
+xml.instruct!
+xml.test
\ No newline at end of file
--- /dev/null
+Hello there,
+
+Mr. <%= @recipient %>
\ No newline at end of file
--- /dev/null
+Hello there,
+
+Mr. <%= @recipient %>. Please see our greeting at <%= @welcome_url %> <%= welcome_url %>
+
+<%= image_tag "somelogo.png" %>
\ No newline at end of file