1 Dr Alexiei Dingli Web Science Stream Advanced ROR.

Slides:



Advertisements
Similar presentations
Chapter 6 Server-side Programming: Java Servlets
Advertisements

PHP Form and File Handling
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 15 Introduction to Rails.
PHP I.
Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
CGI & HTML forms CGI Common Gateway Interface  A web server is only a pipe between user-agents  and content – it does not generate content.
Presenter: James Huang Date: Sept. 29,  HTTP and WWW  Bottle Web Framework  Request Routing  Sending Static Files  Handling HTML  HTTP Errors.
The Librarian Web Page Carol Wolf CS396X. Create new controller  To create a new controller that can manage more than just books, type ruby script/generate.
Introduction to MVC Adding a View Page NTPCUG Tom Perkins, Ph.D.
1.  Understanding about How to Working with Server Side Scripting using PHP Framework (CodeIgniter) 2.
28-Apr-15 Unit Testing in Rails Testing Database Actions.
JavaScript Forms Form Validation Cookies CGI Programs.
Week 2 IBS 685. Static Page Architecture The user requests the page by typing a URL in a browser The Browser requests the page from the Web Server The.
Ruby on Rails Creating a Rails Application Carol E Wolf CS396X.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
Ruby on Rails CSCI 6314 David Gaspar Jennifer Garcia Avila.
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
JSP Standard Tag Library
Lecture 7 – Form processing (Part 2) SFDV3011 – Advanced Web Development 1.
1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
CHAPTER 12 COOKIES AND SESSIONS. INTRO HTTP is a stateless technology Each page rendered by a browser is unrelated to other pages – even if they are from.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
Server-side Scripting Powering the webs favourite services.
The Art of Debugging Shlomy Gantz 02/13/01MDCFUG.
What is Sure BDCs? BDC stands for Batch Data Communication and is also known as Batch Input. It is a technique for mass input of data into SAP by simulating.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
1 Dr Alexiei Dingli Web Science Stream Helpers, Forms and Layouts.
Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4.
Chapter 8 Cookies And Security JavaScript, Third Edition.
Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for.
Introduction to Entity Framework Part 2 CRUD Scaffolding Tom Perkins NTPCUG.
Ruby on Rails CSE 190M, Spring 2009 Week 6. Overview How to use a database Demo creating a blog application on Rails Explain how the application works.
Chapter 6 Server-side Programming: Java Servlets
1 Chapter 9 – Cookies, Sessions, FTP, and More spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
Associations INFO 2310: Topics in Web Design and Programming.
1 Dr Alexiei Dingli Web Science Stream A ROR Blog.
Photo Gallery INFO 2310: Topics in Web Design and Programming.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
1 Dr Alexiei Dingli Web Science Stream A ROR Twitter.
Selenium and Selenium on Rails. Agenda  Overview of Selenium Simple Selenium Tests Selenium IDE  Overview of Selenium on Rails  Problems with Selenium.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
Topics Sending an Multipart message Storing images Getting confirmation Session tracking using PHP Graphics Input Validators Cookies.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Cookies and Sessions IDIA 618 Fall 2014 Bridget M. Blodgett.
Rails & Ajax Module 5. Introduction to Rails Overview of Rails Rails is Ruby based “A development framework for Web-based applications” Rails uses the.
Testing Carol Wolf Computer Science. Testing built into Rails  Rails comes with three databases.  development  test  production  The test database.
 Registry itself is easy and straightforward in implementation  The objects of registry are actually complicated to store and manage  Objects of Registry.
The Problem of State. We will look at… Sometimes web development is just plain weird! Internet / World Wide Web Aspects of their operation The role of.
>> PHP: Insert Query & Form Processing. Insert Query Step 1: Define Form Variables Step 2: Make DB Connection Step 3: Error Handling Step 4: Define the.
PHP Error Handling & Reporting. Error Handling Never allow a default error message or error number returned by the mysql_error() and mysql_errno() functions.
ICM – API Server & Forms Gary Ratcliffe.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
ASP-2-1 SERVER AND CLIENT SIDE SCRITPING Colorado Technical University IT420 Tim Peterson.
CS 160 and CMPE/SE 131 Software Engineering February 9 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
Unit Testing with FlexUnit
Text INTRODUCTION TO ASP.NET. InterComm Campaign Guidelines CONFIDENTIAL Simply Server side language Simplified page development model Modular, well-factored,
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
Ruby on Rails. Web Framework for Ruby Designed to make it easier to develop, deploy, and maintain web applications Design with Model-View-Controller –almost.
Cool CF Debugging Shlomy Gantz 07/29/01CF_ODYSSEY.
National College of Science & Information Technology.
Node.js Express Web Applications
Play Framework: Introduction
Unit Testing.
Chapter 15 Introduction to Rails.
JUnit Reading: various web pages
Presentation transcript:

1 Dr Alexiei Dingli Web Science Stream Advanced ROR

2 Its easy Ensures code still runs after major changes Can simulate browser requests thus making them more convenient Why should we test?

3 Fixtures –Test data Functional –Test controllers Integration –Test number of controllers interacting together Unit –Test models test_helper.rb –is the default tests configuration Rails’ test folder

4 Sample data Created automatically when the generate command is issued Normally have a YAML extension Fixtures

5 myTest1: name: Jack birthday: profession: Systems development myOtherOldTest: name: Thomas birthday: profession: Systems engineer YAML

6 Tom: year_of_birth: YAML spiced with Ruby

7 script/generate scaffold post title:string body:text Results in... create test/unit/post_test.rb create test/fixtures/posts.yml Unit Testing

8 require 'test_helper' class PostTest < ActiveSupport::TestCase # Replace this with your real tests. def test_truth assert true end test/unit/post_test.rb

9 require 'test_helper' # default configuration class PostTest < ActiveSupport::TestCase # Replace this with your real tests. def test_truth assert true end test/unit/post_test.rb

10 require 'test_helper' class PostTest < ActiveSupport::TestCase # inherits ActiveSupport::TestCase # Replace this with your real tests. def test_truth assert true end test/unit/post_test.rb

11 require 'test_helper' class PostTest < ActiveSupport::TestCase # Replace this with your real tests. def test_truth # any test case must start with a test assert true end test/unit/post_test.rb

12 require 'test_helper' class PostTest < ActiveSupport::TestCase # Replace this with your real tests. def test_truth assert true # an assertion evaluates an object end test/unit/post_test.rb

13 rake db:migrate # ensure the current database is loaded rake db:test:load # recreates the test database from the current schema cd test ruby unit/post_test.rb # just run the test! Let’s test!

14 ruby unit/post_test.rb -n test_truth test_truth is the method name which is invoked To run just one test case use...

15 def test_should_not_save_post_without_title post = Post.new assert !post.save end The post saves so the assertion is false A failing test...

16 def test_should_not_save_post_without_title post = Post.new assert !post.save, "Saved the post without a title" end A customised message test

17 In your model... class Post < ActiveRecord::Base validates_presence_of :title end Lets arrange the failing test

18 AssertionsPurpose assert( boolean, [msg] ) Ensures that the object/expression is true. assert_equal( obj1, obj2, [msg] )Ensures that obj1 == obj2 is true. assert_not_equal( obj1, obj2, [msg] )Ensures that obj1 == obj2 is false. assert_same( obj1, obj2, [msg] )Ensures that obj1.equal?(obj2) is true. assert_not_same( obj1, obj2, [msg] )Ensures that obj1.equal?(obj2) is false. assert_nil( obj, [msg] )Ensures that obj.nil? is true. assert_not_nil( obj, [msg] )Ensures that obj.nil? is false. assert_match( regexp, string, [msg] ) Ensures that a string matches the regular expression. assert_no_match( regexp, string, [msg] ) Ensures that a string doesn’t matches the regular expression. assert_in_delta( expecting, actual, delta, [msg] ) Ensures that the numbers expecting and actual are within delta of each other. assert_throws( symbol, [msg] ) { block } Ensures that the given block throws the symbol.

19 AssertionsPurpose assert_raise( exception1, exception2,... ) { block } Ensures that the given block raises one of the given exceptions. assert_nothing_raised( exception1, exception2,... ) { block } Ensures that the given block doesn’t raise one of the given exceptions. assert_instance_of( class, obj, [msg] )Ensures that obj is of the class type. assert_kind_of( class, obj, [msg] ) Ensures that obj is or descends from class. assert_respond_to( obj, symbol, [msg] ) Ensures that obj has a method called symbol. assert_operator( obj1, operator, obj2, [msg] ) Ensures that obj1.operator(obj2) is true. assert_send( array, [msg] ) Ensures that executing the method listed in array[1] on the object in array[0] with the parameters of array[2 and up] is true. This one is weird eh? flunk( [msg] ) Ensures failure. This is useful to explicitly mark a test that isn’t finished yet.

20 Rails AssertionsPurpose assert_valid(record) Ensures that the passed record is valid by Active Record standards and returns any error messages if it is not. assert_difference(expressions, difference = 1, message = nil) {...} Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block. assert_no_difference(expressions, message = nil, &block) Asserts that the numeric result of evaluating an expression is not changed before and after invoking the passed in block. assert_recognizes(expected_options, path, extras={}, message=nil) Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. Basically, it asserts that Rails recognizes the route given by expected_options. assert_generates(expected_path, options, defaults={}, extras = {}, message=nil) Asserts that the provided options can be used to generate the provided path. This is the inverse of assert_recognizes. The extras parameter is used to tell the request the names and values of additional request parameters that would be in a query string. The message parameter allows you to specify a custom error message for assertion failures. assert_response(type, message = nil) Asserts that the response comes with a specific status code. You can specify:success to indicate 200, :redirect to indicate , :missing to indicate 404, or :error to match the range assert_redirected_to(options = {}, message=nil) Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, such thatassert_redirected_to(:controller => "weblog") will also match the redirection ofredirect_to(:controller => "weblog", :action => "show") and so on. assert_template(expected = nil, message=nil) Asserts that the request was rendered with the appropriate template file.

21 Tests controllers such as... –was the web request successful? –was the user redirected to the right page? –was the user successfully authenticated? –was the correct object stored in the response template? –was the appropriate message displayed to the user in the view Functional Tests

22 def test_should_get_index get :index # simulates a request to the action index assert_response :success # ensure the request is successful assert_not_nil assigns(:posts) # ensure it assignes a valid post instance end Getting to the index page

23 get post put delete Requests type

24 Assigns –Any objects that are stored as instance variables in actions for use in views. Cookies –Any cookies that are set. Flash –Any objects living in the flash. Session –Any object living in session variables. Hashes you can use

25 Asserting ViewsPurpose assert_select_ Allows you to make assertions on the body of an . assert_select_rjs Allows you to make assertions on RJS response.assert_select_rjs ha s variants which allow you to narrow down on the updated element or even a particular operation on an element. assert_select_encoded Allows you to make assertions on encoded HTML. It does this by un- encoding the contents of each element and then calling the block with all the un-encoded elements. css_select(selector)orcss_select(elem ent, selector) Returns an array of all the elements selected by the selector. In the second variant it first matches the base element and tries to match the selector expression on any of its children. If there are no matches both variants return an empty array.

26 Used to test interaction between several components General focus on work flow Each integration test has to be explicitly defined Integration testing

27 script/generate integration_test user_flows Creating the tests...

28 require 'test_helper' class UserFlowsTest < ActionController::IntegrationTest # fixtures :your, :models # Replace this with your real tests. def test_truth assert true end Modifying the tests... test/integration/user_flows_test.rb

29 Integration helpersPurpose https? Returns true if the session is mimicking a secure HTTPS request. https!Allows you to mimic a secure HTTPS request. host! Allows you to set the host name to use in the next request. redirect?Returns true if the last request was a redirect. follow_redirect!Follows a single redirect response. request_via_redirect(http_method, path, [parameters], [headers]) Allows you to make an HTTP request and follow any subsequent redirects. post_via_redirect(path, [parameters], [headers]) Allows you to make an HTTP POST request and follow any subsequent redirects. get_via_redirect(path, [parameters], [headers]) Allows you to make an HTTP GET request and follow any subsequent redirects. put_via_redirect(path, [parameters], [headers]) Allows you to make an HTTP PUT request and follow any subsequent redirects. delete_via_redirect(path, [parameters], [headers]) Allows you to make an HTTP DELETErequest and follow any subsequent redirects. open_sessionOpens a new session instance.

30 Questions?