test development forgery prevention rubular

we will start with mostly static pages
we will be using respec for generating the test , however the default
test  called test unit
so to suppress test unit we use  --skip-test-unit and this would prevent
rails to generate the default test directory

rails new sample_app --skip-test-unit

configure the right gems in gemsfile
now create rspec configuration file by doing

rails generate rspec:install

rails has two ways of handling static pages
first is serving static html page from public directory. Static html
means just the html file no embeded ruby or java or anything. just add a
new file to public folder and name is filename.html and that would be
static html page
now the other way to create html file is through rails stack in view and
controller part. For our app we will have home page, about page and a
contact page. Since we have static pages , we would like to have a
controller with one action per page by running the command

rails generate controller Pages home contact

we will intentionally forget about the about page and add it manually
later
now look at the config\routes.rb now instead of resources we have get
request defined for
home and contact pages.
now do http:/localhost:3000/pages/home.html
this brings up the pages in app/views/pages/home.html.erb
if you delete the home method from the controllers file right now and go
the http:/localhost:3000/pages/home.html link , the rails will still
bring up the page by implicit actions.
we are doing teset driven development and we will write test before
making the application
for this purpose we will write a test for About page and then we will
see the test fail
now we will remove the unwanted files from the rails application which
gets generated automatically.
so we will remove spec/views/

git rm -r spec/views

and we will remove spec/helpers

git rm -r spec/helpers

we will test our application in controllers views. However every one has
their own preference all it matters is you should test, now if we see
the the spec ditectory  there is 1)Controllers directory and 2)
spec_helper.rb.
In Controllers directory we have Controllers pages_controller_spec.rb.
even if we don't have anyhing yet, to run our test we will do

rpec spec/

this error occured
C:\Users\Aman\Documents\GitHub\rails-projects\sample_app>rspec spec/

C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/bundler-1.6.2/lib/bundler/r
untime.rb:34:in `block in setup': You have already activated rspec-core
3.1.4, b
ut your Gemfile requires rspec-core 2.13.1. Prepending `bundle exec` to
your com
mand may solve this. (Gem::LoadError)
to deal with error execute

gem install rspec -v 2.13.0
add gem minitest to gemfile
run bundle install

Run the spec using

$ bundle exec rspec spec/

This will ensure the command is executed with the same version listed in
your Gemfile.

rspec leads to too much time over head and to reduce it we can install
spork -- I am not doing it.
now we have something called autotest which can we install.
we will now generate test which would lead to failure. now
spec/pages_controller_spec.rb is a ruby file and rpec exploits
maliability of ruby to make a DSL which is domain specific language

describe "GET 'home'" do
it "returns http success" do
get 'home'  <-- get is a function which generates a     get request
response.should be_success <-- success refers to what        do you get
in return you should get some page
end
end
now we create a test
and we go to pages_controller_spec.rb and write a
test for pages controller
describe "GET 'about'" do
it "returns http success" do
get 'about'
response.should be_success
end
end

re run the test
ops,
routes missing
enter the routes
--pages/about
rereun the test
ops,
we haven't defined the about page in pages_controller

def about
end

re run the test
ops,
we don't have a view for about
go to app/view
create about.html
v-19,
Goal: Static pages and a little bit of dynamic page
we will change the titles of the pages about,contact and home
--writing a test in pages_controller_spec.rb
it "should have the right title" do
get 'home'
response.should have_selector("title",
:content => "Ruby on Rails Tutorial Sample App | Home")
end
here have_selector is a method
in which we mention the html tag of which we want to write test for.
and variable content => shoud contain the text and | is pipeline
runs the test,
test fails ops
now we have to edit home.html.erb
runs the test
test fails ops,
however we have made the right page the test fails. The
reason being the fact that we initially removed the feature for testing
views 7 helpers using rspec. Hence we would have to check it using the
controllers page. so to achieve that we will write render_view under the
line
describe PagesController. like this
describe PagesController do
render_views
now we have changed all the html pages and and we run the test -- which
are still failing 0_o and I do not know why.
Any who,
when you go to any of the pages using rails s in the browser, you will
notice that the title on the source code in the browser is sample app.
This is because of the layouts.
go to app/views/layouts/application.html.erb
you will see this  :

<!DOCTYPE html>
<html>
<head>
<title>SampleApp</title>
<%= stylesheet_link_tag    'application', media: 'all',
'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' =>
true %>
<%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

for time being we are getting rid of it but we will bring it back

run command rm app/views/layouts/application.html.erb

now since the application.html.erb is removed go to web browser and
refresh the page. now we have the right title.
test are still failing if you are wondering about it 0_p
notice we have similar pages, everything in the home, about and contact
page is same except the body so we will reduce this duplication. We will
now configure the controller to have our view rendered properly. To do
this the steps are:
we will add something to each page and make the title look almost same,
this will be done using embedded ruby.
go to the pages_controller and do this:
def home
@title = "Home"
end
now I will go to the home.html.erb and do this :
<title>Ruby on Rails Tutorial Sample App | <%= @title %>/title>
now this is dynamic html.
<%= @title %> this inserts the content of @title variable to the html
page from controller.
now do this for all three pages and controller methods
def contact
@title = "Contact"
end

def about
@title = "About"
end
end

NOTE: REMEMBER THE SCOPE OF VARIABLE TITLE IS JUST IN THAT METHOD.
test are still failing :c
now it would be benificial to clump the title of all the pages in one
file since all titles are same in html pages about home and contact
<title>Ruby on Rails Tutorial Sample App | <%= @title %>/title>
to achieve this we will restore application.html.erb which we had
removed previously. this can be done by adding a new file
application.html.erb

now we will cut this :
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App |<%= @title %></title>
</head>
<body>
</body>
</html>
from all the files (home, about , contact)
and add it to application.html.erb
now if we go to the web page in the browser we see nothing,
that is because we don't have anything in body of application.html.erb
file.
so now before doing that we will right the test to make sure the body is
not empty
go to pages_controller_spec.rb and
add this to section of home about and contact page
it "should have a non-blank body" do
get 'home'
response.body.should_not =~//
end
where /<body>\s*<\/body>/ <--- is  regular expression which means
matching text of the body or body content with blank content.
go to rubular check it out its ia a ruby regular expression editors.
now insert the body in application.html.erb using ebedded ruby
we do this by entring  yield
<%= yield %> which is related to ruby block and what we are doing is we
are yielding the details of about home and contact page. so what yiled
would do is insert the content of home contact and about page instead of
yield in application.html.erb
we do not have to write the test for body for each of home contact and
about page because we handle everything though one layout. so we only
make one test.
in rails 3 we have a meta tag:
<%= csrf_meta_tag %>
which is I included in application.html.erb file.
crsf_meta_tag which is a ruby function and is cross site
request forgery.  It prevent a perticular kind of attack.