<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Amaroo</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Amaroo"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Amaroo"/>
	<updated>2026-06-08T21:29:43Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54385</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54385"/>
		<updated>2011-10-30T15:55:12Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems &amp;lt;ref&amp;gt;http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/&amp;lt;/ref&amp;gt;. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Model-View-Controller&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/action_controller_overview.html&amp;lt;/ref&amp;gt;. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/migrations.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt;. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key&amp;lt;ref&amp;gt;http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/&amp;lt;/ref&amp;gt;. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
&lt;br /&gt;
[[File:41.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
&lt;br /&gt;
[[File:42.png|1000px]] &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt;. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Functional_testing&amp;lt;/ref&amp;gt;. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view &amp;lt;ref&amp;gt;http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/&amp;lt;/ref&amp;gt;. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Integration_testing&amp;lt;/ref&amp;gt;. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
===Performance Tests===&lt;br /&gt;
&lt;br /&gt;
Performance tests are used for profiling and benchmarking the test code.  It helps to determine where the applications memory and speed problems are coming from and gives a more in-depth picture of these problems.  &lt;br /&gt;
&lt;br /&gt;
Performance testing is an integral part of development life cycle as you do not want your end users to have a bad experience.  If your application is too slow then the user has to wait for long for each page to be completely loaded. Performance testing helps to give a pleasant browsing experience to end users and also helps in cutting the costs of unnecessary hardware.&lt;br /&gt;
&lt;br /&gt;
Examples can be found [http://guides.rubyonrails.org/performance_testing.html here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In the lecture we have learn how to make a rails application using ruby mine. Also we have learn different types of testing and there usages. Since ruby is a dynamically typed language it makes testing an integral part of the rails application.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54383</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54383"/>
		<updated>2011-10-30T15:54:25Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* Integration Tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems &amp;lt;ref&amp;gt;http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/&amp;lt;/ref&amp;gt;. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Model-View-Controller&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/action_controller_overview.html&amp;lt;/ref&amp;gt;. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/migrations.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt;. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key&amp;lt;ref&amp;gt;http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/&amp;lt;/ref&amp;gt;. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
&lt;br /&gt;
[[File:41.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
&lt;br /&gt;
[[File:42.png|1000px]] &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt;. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Functional_testing&amp;lt;/ref&amp;gt;. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view &amp;lt;ref&amp;gt;http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/&amp;lt;/ref&amp;gt;. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Integration_testing&amp;lt;/ref&amp;gt;. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
===Performance Tests===&lt;br /&gt;
&lt;br /&gt;
Performance tests are used for profiling and benchmarking the test code.  It helps to determine where the applications memory and speed problems are coming from and gives a more in-depth picture of these problems.  &lt;br /&gt;
&lt;br /&gt;
Performance testing is an integral part of development life cycle as you do not want your end users to have a bad experience.  If your application is too slow then the user has to wait for long for each page to be completely loaded. Performance testing helps to give a pleasant browsing experience to end users and also helps in cutting the costs of unnecessary hardware.&lt;br /&gt;
&lt;br /&gt;
Examples can be found [http://guides.rubyonrails.org/performance_testing.html here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In the lecture we have learn how to make a rails application using ruby mine. Also we have learn different types of testing and there usages. Since ruby is a dynamically typed language it makes testing an integral part of the rails application.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;br /&gt;
* 11. [http://ruby.railstutorial.org/ruby-on-rails-tutorial-book Ruby on Rails Tutorial]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54382</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54382"/>
		<updated>2011-10-30T15:53:58Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* Functional Tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems &amp;lt;ref&amp;gt;http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/&amp;lt;/ref&amp;gt;. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Model-View-Controller&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/action_controller_overview.html&amp;lt;/ref&amp;gt;. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/migrations.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt;. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key&amp;lt;ref&amp;gt;http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/&amp;lt;/ref&amp;gt;. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
&lt;br /&gt;
[[File:41.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
&lt;br /&gt;
[[File:42.png|1000px]] &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt;. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Functional_testing&amp;lt;/ref&amp;gt;. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view &amp;lt;ref&amp;gt;http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/&amp;lt;/ref&amp;gt;. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Performance Tests===&lt;br /&gt;
&lt;br /&gt;
Performance tests are used for profiling and benchmarking the test code.  It helps to determine where the applications memory and speed problems are coming from and gives a more in-depth picture of these problems.  &lt;br /&gt;
&lt;br /&gt;
Performance testing is an integral part of development life cycle as you do not want your end users to have a bad experience.  If your application is too slow then the user has to wait for long for each page to be completely loaded. Performance testing helps to give a pleasant browsing experience to end users and also helps in cutting the costs of unnecessary hardware.&lt;br /&gt;
&lt;br /&gt;
Examples can be found [http://guides.rubyonrails.org/performance_testing.html here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In the lecture we have learn how to make a rails application using ruby mine. Also we have learn different types of testing and there usages. Since ruby is a dynamically typed language it makes testing an integral part of the rails application.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;br /&gt;
* 11. [http://ruby.railstutorial.org/ruby-on-rails-tutorial-book Ruby on Rails Tutorial]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54381</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54381"/>
		<updated>2011-10-30T15:53:24Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* Unit tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems &amp;lt;ref&amp;gt;http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/&amp;lt;/ref&amp;gt;. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Model-View-Controller&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/action_controller_overview.html&amp;lt;/ref&amp;gt;. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/migrations.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt;. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key&amp;lt;ref&amp;gt;http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/&amp;lt;/ref&amp;gt;. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
&lt;br /&gt;
[[File:41.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
&lt;br /&gt;
[[File:42.png|1000px]] &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt;. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Performance Tests===&lt;br /&gt;
&lt;br /&gt;
Performance tests are used for profiling and benchmarking the test code.  It helps to determine where the applications memory and speed problems are coming from and gives a more in-depth picture of these problems.  &lt;br /&gt;
&lt;br /&gt;
Performance testing is an integral part of development life cycle as you do not want your end users to have a bad experience.  If your application is too slow then the user has to wait for long for each page to be completely loaded. Performance testing helps to give a pleasant browsing experience to end users and also helps in cutting the costs of unnecessary hardware.&lt;br /&gt;
&lt;br /&gt;
Examples can be found [http://guides.rubyonrails.org/performance_testing.html here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In the lecture we have learn how to make a rails application using ruby mine. Also we have learn different types of testing and there usages. Since ruby is a dynamically typed language it makes testing an integral part of the rails application.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;br /&gt;
* 11. [http://ruby.railstutorial.org/ruby-on-rails-tutorial-book Ruby on Rails Tutorial]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54380</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54380"/>
		<updated>2011-10-30T15:52:50Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* Fixtures */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems &amp;lt;ref&amp;gt;http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/&amp;lt;/ref&amp;gt;. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Model-View-Controller&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/action_controller_overview.html&amp;lt;/ref&amp;gt;. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/migrations.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt;. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key&amp;lt;ref&amp;gt;http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/&amp;lt;/ref&amp;gt;. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
&lt;br /&gt;
[[File:41.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
&lt;br /&gt;
[[File:42.png|1000px]] &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory[http://guides.rubyonrails.org/testing.html]. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Performance Tests===&lt;br /&gt;
&lt;br /&gt;
Performance tests are used for profiling and benchmarking the test code.  It helps to determine where the applications memory and speed problems are coming from and gives a more in-depth picture of these problems.  &lt;br /&gt;
&lt;br /&gt;
Performance testing is an integral part of development life cycle as you do not want your end users to have a bad experience.  If your application is too slow then the user has to wait for long for each page to be completely loaded. Performance testing helps to give a pleasant browsing experience to end users and also helps in cutting the costs of unnecessary hardware.&lt;br /&gt;
&lt;br /&gt;
Examples can be found [http://guides.rubyonrails.org/performance_testing.html here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In the lecture we have learn how to make a rails application using ruby mine. Also we have learn different types of testing and there usages. Since ruby is a dynamically typed language it makes testing an integral part of the rails application.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;br /&gt;
* 11. [http://ruby.railstutorial.org/ruby-on-rails-tutorial-book Ruby on Rails Tutorial]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54379</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54379"/>
		<updated>2011-10-30T15:51:39Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* Migrations */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems &amp;lt;ref&amp;gt;http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/&amp;lt;/ref&amp;gt;. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Model-View-Controller&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/action_controller_overview.html&amp;lt;/ref&amp;gt;. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/migrations.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data[http://ar.rubyonrails.org/classes/Fixtures.html]. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key[http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/]. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
&lt;br /&gt;
[[File:41.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
&lt;br /&gt;
[[File:42.png|1000px]] &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory[http://guides.rubyonrails.org/testing.html]. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Performance Tests===&lt;br /&gt;
&lt;br /&gt;
Performance tests are used for profiling and benchmarking the test code.  It helps to determine where the applications memory and speed problems are coming from and gives a more in-depth picture of these problems.  &lt;br /&gt;
&lt;br /&gt;
Performance testing is an integral part of development life cycle as you do not want your end users to have a bad experience.  If your application is too slow then the user has to wait for long for each page to be completely loaded. Performance testing helps to give a pleasant browsing experience to end users and also helps in cutting the costs of unnecessary hardware.&lt;br /&gt;
&lt;br /&gt;
Examples can be found [http://guides.rubyonrails.org/performance_testing.html here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In the lecture we have learn how to make a rails application using ruby mine. Also we have learn different types of testing and there usages. Since ruby is a dynamically typed language it makes testing an integral part of the rails application.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;br /&gt;
* 11. [http://ruby.railstutorial.org/ruby-on-rails-tutorial-book Ruby on Rails Tutorial]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54378</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54378"/>
		<updated>2011-10-30T15:50:53Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* Model, View and Controller */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems &amp;lt;ref&amp;gt;http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/&amp;lt;/ref&amp;gt;. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Model-View-Controller&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/action_controller_overview.html&amp;lt;/ref&amp;gt;. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema [http://guides.rubyonrails.org/migrations.html]. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data[http://ar.rubyonrails.org/classes/Fixtures.html]. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key[http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/]. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
&lt;br /&gt;
[[File:41.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
&lt;br /&gt;
[[File:42.png|1000px]] &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory[http://guides.rubyonrails.org/testing.html]. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Performance Tests===&lt;br /&gt;
&lt;br /&gt;
Performance tests are used for profiling and benchmarking the test code.  It helps to determine where the applications memory and speed problems are coming from and gives a more in-depth picture of these problems.  &lt;br /&gt;
&lt;br /&gt;
Performance testing is an integral part of development life cycle as you do not want your end users to have a bad experience.  If your application is too slow then the user has to wait for long for each page to be completely loaded. Performance testing helps to give a pleasant browsing experience to end users and also helps in cutting the costs of unnecessary hardware.&lt;br /&gt;
&lt;br /&gt;
Examples can be found [http://guides.rubyonrails.org/performance_testing.html here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In the lecture we have learn how to make a rails application using ruby mine. Also we have learn different types of testing and there usages. Since ruby is a dynamically typed language it makes testing an integral part of the rails application.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;br /&gt;
* 11. [http://ruby.railstutorial.org/ruby-on-rails-tutorial-book Ruby on Rails Tutorial]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54377</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54377"/>
		<updated>2011-10-30T15:49:47Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems &amp;lt;ref&amp;gt;http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/&amp;lt;/ref&amp;gt;. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Model-View-Controller&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules[http://guides.rubyonrails.org/action_controller_overview.html]. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema [http://guides.rubyonrails.org/migrations.html]. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data[http://ar.rubyonrails.org/classes/Fixtures.html]. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key[http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/]. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
&lt;br /&gt;
[[File:41.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
&lt;br /&gt;
[[File:42.png|1000px]] &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory[http://guides.rubyonrails.org/testing.html]. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Performance Tests===&lt;br /&gt;
&lt;br /&gt;
Performance tests are used for profiling and benchmarking the test code.  It helps to determine where the applications memory and speed problems are coming from and gives a more in-depth picture of these problems.  &lt;br /&gt;
&lt;br /&gt;
Performance testing is an integral part of development life cycle as you do not want your end users to have a bad experience.  If your application is too slow then the user has to wait for long for each page to be completely loaded. Performance testing helps to give a pleasant browsing experience to end users and also helps in cutting the costs of unnecessary hardware.&lt;br /&gt;
&lt;br /&gt;
Examples can be found [http://guides.rubyonrails.org/performance_testing.html here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In the lecture we have learn how to make a rails application using ruby mine. Also we have learn different types of testing and there usages. Since ruby is a dynamically typed language it makes testing an integral part of the rails application.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;br /&gt;
* 11. [http://ruby.railstutorial.org/ruby-on-rails-tutorial-book Ruby on Rails Tutorial]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54376</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54376"/>
		<updated>2011-10-30T15:48:14Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems &amp;lt;ref&amp;gt;http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/&amp;lt;/ref&amp;gt;. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Model-View-Controller&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules[http://guides.rubyonrails.org/action_controller_overview.html]. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema [http://guides.rubyonrails.org/migrations.html]. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data[http://ar.rubyonrails.org/classes/Fixtures.html]. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key[http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/]. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
&lt;br /&gt;
[[File:41.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
&lt;br /&gt;
[[File:42.png|1000px]] &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory[http://guides.rubyonrails.org/testing.html]. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Performance Tests===&lt;br /&gt;
&lt;br /&gt;
Performance tests are used for profiling and benchmarking the test code.  It helps to determine where the applications memory and speed problems are coming from and gives a more in-depth picture of these problems.  &lt;br /&gt;
&lt;br /&gt;
Performance testing is an integral part of development life cycle as you do not want your end users to have a bad experience.  If your application is too slow then the user has to wait for long for each page to be completely loaded. Performance testing helps to give a pleasant browsing experience to end users and also helps in cutting the costs of unnecessary hardware.&lt;br /&gt;
&lt;br /&gt;
Examples can be found [http://guides.rubyonrails.org/performance_testing.html here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In the lecture we have learn how to make a rails application using ruby mine. Also we have learn different types of testing and there usages. Since ruby is a dynamically typed language it makes testing an integral part of the rails application.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;br /&gt;
* 11. [http://ruby.railstutorial.org/ruby-on-rails-tutorial-book Ruby on Rails Tutorial]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54375</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54375"/>
		<updated>2011-10-30T15:47:00Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* View */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems &amp;lt;ref&amp;gt;http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/&amp;lt;/ref&amp;gt;. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Model-View-Controller&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules[http://guides.rubyonrails.org/action_controller_overview.html]. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema [http://guides.rubyonrails.org/migrations.html]. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data[http://ar.rubyonrails.org/classes/Fixtures.html]. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key[http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/]. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
&lt;br /&gt;
[[File:41.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
&lt;br /&gt;
[[File:42.png|1000px]] &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory[http://guides.rubyonrails.org/testing.html]. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Performance Tests===&lt;br /&gt;
&lt;br /&gt;
Performance tests are used for profiling and benchmarking the test code.  It helps to determine where the applications memory and speed problems are coming from and gives a more in-depth picture of these problems.  &lt;br /&gt;
&lt;br /&gt;
Performance testing is an integral part of development life cycle as you do not want your end users to have a bad experience.  If your application is too slow then the user has to wait for long for each page to be completely loaded. Performance testing helps to give a pleasant browsing experience to end users and also helps in cutting the costs of unnecessary hardware.&lt;br /&gt;
&lt;br /&gt;
Examples can be found [http://guides.rubyonrails.org/performance_testing.html here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In the lecture we have learn how to make a rails application using ruby mine. Also we have learn different types of testing and there usages. Since ruby is a dynamically typed language it makes testing an integral part of the rails application.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/ Intermediate Rails: Understanding Models, Views and Controllers]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;br /&gt;
* 11. [http://ruby.railstutorial.org/ruby-on-rails-tutorial-book Ruby on Rails Tutorial]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54374</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54374"/>
		<updated>2011-10-30T15:45:50Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* Model */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems &amp;lt;ref&amp;gt;http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/&amp;lt;/ref&amp;gt;. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend[http://en.wikipedia.org/wiki/Model-View-Controller].&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules[http://guides.rubyonrails.org/action_controller_overview.html]. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema [http://guides.rubyonrails.org/migrations.html]. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data[http://ar.rubyonrails.org/classes/Fixtures.html]. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key[http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/]. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
&lt;br /&gt;
[[File:41.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
&lt;br /&gt;
[[File:42.png|1000px]] &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory[http://guides.rubyonrails.org/testing.html]. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Performance Tests===&lt;br /&gt;
&lt;br /&gt;
Performance tests are used for profiling and benchmarking the test code.  It helps to determine where the applications memory and speed problems are coming from and gives a more in-depth picture of these problems.  &lt;br /&gt;
&lt;br /&gt;
Performance testing is an integral part of development life cycle as you do not want your end users to have a bad experience.  If your application is too slow then the user has to wait for long for each page to be completely loaded. Performance testing helps to give a pleasant browsing experience to end users and also helps in cutting the costs of unnecessary hardware.&lt;br /&gt;
&lt;br /&gt;
Examples can be found [http://guides.rubyonrails.org/performance_testing.html here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In the lecture we have learn how to make a rails application using ruby mine. Also we have learn different types of testing and there usages. Since ruby is a dynamically typed language it makes testing an integral part of the rails application.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/ Intermediate Rails: Understanding Models, Views and Controllers]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;br /&gt;
* 11. [http://ruby.railstutorial.org/ruby-on-rails-tutorial-book Ruby on Rails Tutorial]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54373</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54373"/>
		<updated>2011-10-30T15:43:47Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* Model */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems &amp;lt;ref&amp;gt; http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/&amp;lt;/ref&amp;gt;. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend[http://en.wikipedia.org/wiki/Model-View-Controller].&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules[http://guides.rubyonrails.org/action_controller_overview.html]. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema [http://guides.rubyonrails.org/migrations.html]. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data[http://ar.rubyonrails.org/classes/Fixtures.html]. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key[http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/]. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
&lt;br /&gt;
[[File:41.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
&lt;br /&gt;
[[File:42.png|1000px]] &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory[http://guides.rubyonrails.org/testing.html]. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Performance Tests===&lt;br /&gt;
&lt;br /&gt;
Performance tests are used for profiling and benchmarking the test code.  It helps to determine where the applications memory and speed problems are coming from and gives a more in-depth picture of these problems.  &lt;br /&gt;
&lt;br /&gt;
Performance testing is an integral part of development life cycle as you do not want your end users to have a bad experience.  If your application is too slow then the user has to wait for long for each page to be completely loaded. Performance testing helps to give a pleasant browsing experience to end users and also helps in cutting the costs of unnecessary hardware.&lt;br /&gt;
&lt;br /&gt;
Examples can be found [http://guides.rubyonrails.org/performance_testing.html here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In the lecture we have learn how to make a rails application using ruby mine. Also we have learn different types of testing and there usages. Since ruby is a dynamically typed language it makes testing an integral part of the rails application.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/ Intermediate Rails: Understanding Models, Views and Controllers]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;br /&gt;
* 11. [http://ruby.railstutorial.org/ruby-on-rails-tutorial-book Ruby on Rails Tutorial]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54372</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54372"/>
		<updated>2011-10-30T15:41:46Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* Model */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems &amp;lt;ref&amp;gt;http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/&amp;lt;/ref&amp;gt;. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend[http://en.wikipedia.org/wiki/Model-View-Controller].&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules[http://guides.rubyonrails.org/action_controller_overview.html]. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema [http://guides.rubyonrails.org/migrations.html]. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data[http://ar.rubyonrails.org/classes/Fixtures.html]. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key[http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/]. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
&lt;br /&gt;
[[File:41.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
&lt;br /&gt;
[[File:42.png|1000px]] &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory[http://guides.rubyonrails.org/testing.html]. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Performance Tests===&lt;br /&gt;
&lt;br /&gt;
Performance tests are used for profiling and benchmarking the test code.  It helps to determine where the applications memory and speed problems are coming from and gives a more in-depth picture of these problems.  &lt;br /&gt;
&lt;br /&gt;
Performance testing is an integral part of development life cycle as you do not want your end users to have a bad experience.  If your application is too slow then the user has to wait for long for each page to be completely loaded. Performance testing helps to give a pleasant browsing experience to end users and also helps in cutting the costs of unnecessary hardware.&lt;br /&gt;
&lt;br /&gt;
Examples can be found [http://guides.rubyonrails.org/performance_testing.html here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In the lecture we have learn how to make a rails application using ruby mine. Also we have learn different types of testing and there usages. Since ruby is a dynamically typed language it makes testing an integral part of the rails application.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/ Intermediate Rails: Understanding Models, Views and Controllers]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;br /&gt;
* 11. [http://ruby.railstutorial.org/ruby-on-rails-tutorial-book Ruby on Rails Tutorial]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54371</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54371"/>
		<updated>2011-10-30T15:37:29Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* Fixtures */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/]. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend[http://en.wikipedia.org/wiki/Model-View-Controller].&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules[http://guides.rubyonrails.org/action_controller_overview.html]. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema [http://guides.rubyonrails.org/migrations.html]. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data[http://ar.rubyonrails.org/classes/Fixtures.html]. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key[http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/]. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
&lt;br /&gt;
[[File:41.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
&lt;br /&gt;
[[File:42.png|1000px]] &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory[http://guides.rubyonrails.org/testing.html]. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Performance Tests===&lt;br /&gt;
&lt;br /&gt;
Performance tests are used for profiling and benchmarking the test code.  It helps to determine where the applications memory and speed problems are coming from and gives a more in-depth picture of these problems.  &lt;br /&gt;
&lt;br /&gt;
Performance testing is an integral part of development life cycle as you do not want your end users to have a bad experience.  If your application is too slow then the user has to wait for long for each page to be completely loaded. Performance testing helps to give a pleasant browsing experience to end users and also helps in cutting the costs of unnecessary hardware.&lt;br /&gt;
&lt;br /&gt;
Examples can be found [http://guides.rubyonrails.org/performance_testing.html here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In the lecture we have learn how to make a rails application using ruby mine. Also we have learn different types of testing and there usages. Since ruby is a dynamically typed language it makes testing an integral part of the rails application.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/ Intermediate Rails: Understanding Models, Views and Controllers]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;br /&gt;
* 11. [http://ruby.railstutorial.org/ruby-on-rails-tutorial-book Ruby on Rails Tutorial]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:42.png&amp;diff=54370</id>
		<title>File:42.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:42.png&amp;diff=54370"/>
		<updated>2011-10-30T15:36:07Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54369</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54369"/>
		<updated>2011-10-30T15:30:44Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* Performance Tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/]. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend[http://en.wikipedia.org/wiki/Model-View-Controller].&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules[http://guides.rubyonrails.org/action_controller_overview.html]. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema [http://guides.rubyonrails.org/migrations.html]. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data[http://ar.rubyonrails.org/classes/Fixtures.html]. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key[http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/]. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
&lt;br /&gt;
[[File:41.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
 &lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory[http://guides.rubyonrails.org/testing.html]. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Performance Tests===&lt;br /&gt;
&lt;br /&gt;
Performance tests are used for profiling and benchmarking the test code.  It helps to determine where the applications memory and speed problems are coming from and gives a more in-depth picture of these problems.  &lt;br /&gt;
&lt;br /&gt;
Performance testing is an integral part of development life cycle as you do not want your end users to have a bad experience.  If your application is too slow then the user has to wait for long for each page to be completely loaded. Performance testing helps to give a pleasant browsing experience to end users and also helps in cutting the costs of unnecessary hardware.&lt;br /&gt;
&lt;br /&gt;
Examples can be found [http://guides.rubyonrails.org/performance_testing.html here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In the lecture we have learn how to make a rails application using ruby mine. Also we have learn different types of testing and there usages. Since ruby is a dynamically typed language it makes testing an integral part of the rails application.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/ Intermediate Rails: Understanding Models, Views and Controllers]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;br /&gt;
* 11. [http://ruby.railstutorial.org/ruby-on-rails-tutorial-book Ruby on Rails Tutorial]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54366</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54366"/>
		<updated>2011-10-30T15:26:35Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* Testing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/]. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend[http://en.wikipedia.org/wiki/Model-View-Controller].&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules[http://guides.rubyonrails.org/action_controller_overview.html]. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema [http://guides.rubyonrails.org/migrations.html]. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data[http://ar.rubyonrails.org/classes/Fixtures.html]. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key[http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/]. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
&lt;br /&gt;
[[File:41.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
 &lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory[http://guides.rubyonrails.org/testing.html]. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Performance Tests===&lt;br /&gt;
&lt;br /&gt;
Performance tests are used for profiling and benchmarking the test code.  It helps to determine where the applications memory and speed problems are coming from and gives a more in-depth picture of these problems.  &lt;br /&gt;
&lt;br /&gt;
Performance testing is an integral part of development life cycle as you do not want your end users to have a bad experience.  If your application is too slow then the user has to wait for long for each page to be completely loaded. Performance testing helps to give a pleasant browsing experience to end users and also helps in cutting the costs of unnecessary hardware.&lt;br /&gt;
&lt;br /&gt;
Examples can be found here[http://guides.rubyonrails.org/performance_testing.html].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In the lecture we have learn how to make a rails application using ruby mine. Also we have learn different types of testing and there usages. Since ruby is a dynamically typed language it makes testing an integral part of the rails application.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/ Intermediate Rails: Understanding Models, Views and Controllers]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;br /&gt;
* 11. [http://ruby.railstutorial.org/ruby-on-rails-tutorial-book Ruby on Rails Tutorial]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54120</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54120"/>
		<updated>2011-10-22T02:30:54Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* Integration Tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/]. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend[http://en.wikipedia.org/wiki/Model-View-Controller].&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules[http://guides.rubyonrails.org/action_controller_overview.html]. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema [http://guides.rubyonrails.org/migrations.html]. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data[http://ar.rubyonrails.org/classes/Fixtures.html]. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key[http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/]. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
&lt;br /&gt;
[[File:41.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
 &lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory[http://guides.rubyonrails.org/testing.html]. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In the lecture we have learn how to make a rails application using ruby mine. Also we have learn different types of testing and there usages. Since ruby is a dynamically typed language it makes testing an integral part of the rails application.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/ Intermediate Rails: Understanding Models, Views and Controllers]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;br /&gt;
* 11. [http://ruby.railstutorial.org/ruby-on-rails-tutorial-book Ruby on Rails Tutorial]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54119</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54119"/>
		<updated>2011-10-22T02:30:32Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* Unit tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/]. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend[http://en.wikipedia.org/wiki/Model-View-Controller].&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules[http://guides.rubyonrails.org/action_controller_overview.html]. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema [http://guides.rubyonrails.org/migrations.html]. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data[http://ar.rubyonrails.org/classes/Fixtures.html]. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key[http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/]. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
&lt;br /&gt;
[[File:41.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
 &lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory[http://guides.rubyonrails.org/testing.html]. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In the lecture we have learn how to make a rails application using ruby mine. Also we have learn different types of testing and there usages. Since ruby is a dynamically typed language it makes testing an integral part of the rails application.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/ Intermediate Rails: Understanding Models, Views and Controllers]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;br /&gt;
* 11. [http://ruby.railstutorial.org/ruby-on-rails-tutorial-book Ruby on Rails Tutorial]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54118</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54118"/>
		<updated>2011-10-22T02:29:58Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/]. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend[http://en.wikipedia.org/wiki/Model-View-Controller].&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules[http://guides.rubyonrails.org/action_controller_overview.html]. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema [http://guides.rubyonrails.org/migrations.html]. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data[http://ar.rubyonrails.org/classes/Fixtures.html]. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key[http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/]. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
&lt;br /&gt;
[[File:41.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
 &lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory[http://guides.rubyonrails.org/testing.html]. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results. &lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In the lecture we have learn how to make a rails application using ruby mine. Also we have learn different types of testing and there usages. Since ruby is a dynamically typed language it makes testing an integral part of the rails application.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/ Intermediate Rails: Understanding Models, Views and Controllers]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;br /&gt;
* 11. [http://ruby.railstutorial.org/ruby-on-rails-tutorial-book Ruby on Rails Tutorial]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54117</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54117"/>
		<updated>2011-10-22T02:28:37Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/]. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend[http://en.wikipedia.org/wiki/Model-View-Controller].&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules[http://guides.rubyonrails.org/action_controller_overview.html]. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema [http://guides.rubyonrails.org/migrations.html]. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data[http://ar.rubyonrails.org/classes/Fixtures.html]. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key[http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/]. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
&lt;br /&gt;
[[File:41.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
 &lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory[http://guides.rubyonrails.org/testing.html]. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results. &lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In the lecture we have learn how to make a rails application using ruby mine. Also we have learn different types of testing and there usages. Since ruby is a dynamically typed language it makes testing an integral part of the rails application.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/ Intermediate Rails: Understanding Models, Views and Controllers]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;br /&gt;
* 11. [http://ruby.railstutorial.org/ruby-on-rails-tutorial-book]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54116</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54116"/>
		<updated>2011-10-22T02:26:48Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/]. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend[http://en.wikipedia.org/wiki/Model-View-Controller].&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules[http://guides.rubyonrails.org/action_controller_overview.html]. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema [http://guides.rubyonrails.org/migrations.html]. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data[http://ar.rubyonrails.org/classes/Fixtures.html]. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key[http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/]. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
&lt;br /&gt;
[[File:41.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
 &lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory[http://guides.rubyonrails.org/testing.html]. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results. &lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In the lecture we have learn how to make a rails application using ruby mine. Also we have learn different types of testing and there usages. Since ruby is a dynamically typed language it makes testing an integral part of the rails application.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/ Intermediate Rails: Understanding Models, Views and Controllers]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54115</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54115"/>
		<updated>2011-10-22T02:06:58Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* Fixtures */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/]. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend[http://en.wikipedia.org/wiki/Model-View-Controller].&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules[http://guides.rubyonrails.org/action_controller_overview.html]. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema [http://guides.rubyonrails.org/migrations.html]. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data[http://ar.rubyonrails.org/classes/Fixtures.html]. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key[http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/]. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
&lt;br /&gt;
[[File:41.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
 &lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory[http://guides.rubyonrails.org/testing.html]. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results. &lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/ Intermediate Rails: Understanding Models, Views and Controllers]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:41.png&amp;diff=54114</id>
		<title>File:41.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:41.png&amp;diff=54114"/>
		<updated>2011-10-22T02:06:30Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54113</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54113"/>
		<updated>2011-10-22T02:05:17Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* Fixtures */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/]. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend[http://en.wikipedia.org/wiki/Model-View-Controller].&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules[http://guides.rubyonrails.org/action_controller_overview.html]. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema [http://guides.rubyonrails.org/migrations.html]. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data[http://ar.rubyonrails.org/classes/Fixtures.html]. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
&lt;br /&gt;
[[File:40.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key[http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/]. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
 &lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory[http://guides.rubyonrails.org/testing.html]. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results. &lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/ Intermediate Rails: Understanding Models, Views and Controllers]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:40.png&amp;diff=54112</id>
		<title>File:40.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:40.png&amp;diff=54112"/>
		<updated>2011-10-22T02:04:38Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54111</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54111"/>
		<updated>2011-10-22T02:02:22Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* Unit tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/]. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend[http://en.wikipedia.org/wiki/Model-View-Controller].&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules[http://guides.rubyonrails.org/action_controller_overview.html]. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema [http://guides.rubyonrails.org/migrations.html]. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data[http://ar.rubyonrails.org/classes/Fixtures.html]. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key[http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/]. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
 &lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory[http://guides.rubyonrails.org/testing.html]. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:Fixtures.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results. &lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/ Intermediate Rails: Understanding Models, Views and Controllers]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54110</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54110"/>
		<updated>2011-10-22T02:01:26Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* Unit tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/]. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend[http://en.wikipedia.org/wiki/Model-View-Controller].&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules[http://guides.rubyonrails.org/action_controller_overview.html]. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema [http://guides.rubyonrails.org/migrations.html]. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data[http://ar.rubyonrails.org/classes/Fixtures.html]. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key[http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/]. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
 &lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory[http://guides.rubyonrails.org/testing.html]. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
&lt;br /&gt;
[[File:44.png|1000px]]&lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results. &lt;br /&gt;
&lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/ Intermediate Rails: Understanding Models, Views and Controllers]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54109</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e ar</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_ar&amp;diff=54109"/>
		<updated>2011-10-22T01:59:58Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: /* Fixtures */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;'''Lecture 10'''&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Model, View and Controller==&lt;br /&gt;
===Model===&lt;br /&gt;
'''Definition''': The model represents all the data in the application. &lt;br /&gt;
&lt;br /&gt;
A model is typically mapped to a table in the database. It maintains the state of an application and responds to changes to alter it. The model provides the developer with a consistent interface to handle and manipulate the data in an application. It hides a object-relation-mapping underneath, allowing the application to be portable to various database management systems [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/]. The model also validates the data before storing it. The code for a model can be found in the ''apps/models'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===View===&lt;br /&gt;
'''Definition''': A view renders the model in a form suitable for the user to comprehend[http://en.wikipedia.org/wiki/Model-View-Controller].&lt;br /&gt;
&lt;br /&gt;
A view queries the model to obtain data and generates an interface to display it. Typically, the controller instructs a view to render itself. However, in some cases, the view is automatically notified by the model of changes in state. The view can render itself to the user in many ways, HTML, CSS, XML, Javascript, JSON are the common ones. The code for the views can be found in the ''app/views'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
===Controller===&lt;br /&gt;
'''Definition''': A controller accepts input from the user and instructs the rest of the framework to respond to it.&lt;br /&gt;
&lt;br /&gt;
Typically, the user input is modeled as an event and the controller converts it to a user action, understandable by the model. Based on the input, it also decides which views to render. The controller is also responsible for a number of auxiliary services like session management, caching and helper modules[http://guides.rubyonrails.org/action_controller_overview.html]. Code for a controller can be found in the ''app/controllers/'' directory of a rails project.&lt;br /&gt;
&lt;br /&gt;
==A Rails Project==&lt;br /&gt;
* [http://www.jetbrains.com/ruby/ RubyMine] is an IDE for Ruby. We will be developing out rails project using it.&lt;br /&gt;
&lt;br /&gt;
===Start a rails project===&lt;br /&gt;
* To start a new rails project, click on the &amp;quot;New Project” link or select it from File Menu.&lt;br /&gt;
* Enter the project name, location and its type. In this case, it is a 'Rails application'.&lt;br /&gt;
[[File:Ch43-ar-1.PNG‎]]&lt;br /&gt;
* Select the ruby interpreter you want to use.&lt;br /&gt;
* Select the rails version. There are significant differences among different rails version, so it is important to select the correct version.&lt;br /&gt;
* Select a JavaScript library for your views.&lt;br /&gt;
* Rails can work with a variety of databases like [http://www.sqlite.org/ SQLite], [http://www.mysql.com/ MySQL], [http://www.postgresql.org/ PostgreSQL]. Select the one that you want from the drop down menu. We use SQLite3 for this project&lt;br /&gt;
[[File:Ch4e-ar-2.PNG‎]]&lt;br /&gt;
* Click Ok to finish your initial project configuration&lt;br /&gt;
* You can see Ruby Mine executes a command for you. If the projects gets successfully created then you will see “Process finished with exit code 0”.&lt;br /&gt;
[[File:Ch4e-ar-3.png|1000px]]&lt;br /&gt;
* Once the project gets created it is important to test if your project is running. To do this, start the server by pressing the green button on the top. On doing so, you should see a welcome screen if the server was started successfully.&lt;br /&gt;
[[File:Ch4e-ar-5.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The three things===&lt;br /&gt;
Before you start actual development of your application, there are three things that you need to do.&lt;br /&gt;
* Remove the index.html file from the public directory of your rails project. This is a static file, and static files are given preference over dynamic ones. Hence unless this file is deleted, it will the home page of your application. &lt;br /&gt;
* Modify the default route in the config/routes.rb file to point to the page that you want to be the home page of your application.&lt;br /&gt;
* Use rake to create tables in your database corresponding to your models (explained in subsequent section).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Use scaffolds to build===&lt;br /&gt;
* Now generate the other components of the project. This is done using scaffolds. &lt;br /&gt;
* 'Scaffolding' is rails way to models, views, and controllers for a new resource in a single operation.&lt;br /&gt;
* To generate a scaffold, go to “Tools” and use “Run Rails Generator”.  You will be prompted with what type of generator you want.&lt;br /&gt;
* Type Scaffold and press enter.&lt;br /&gt;
* Enter the scaffold name and the attributes along with their data types.  Scaffold name should be singular. &lt;br /&gt;
* Attributes and data types are inserted as &amp;lt;Atrribute_name&amp;gt;:&amp;lt;data type&amp;gt;. Multiple attributes can be space separated.&lt;br /&gt;
[[File:Ch4e-ar-6.png|1000px]]&lt;br /&gt;
* Scaffolding provides the application developer with a template that has some of the common operations implemented. The developers needs to extend the scaffold by adding functionality required by that specific application&lt;br /&gt;
* For this project, we have created a scaffold for the 'category' resource.&lt;br /&gt;
[[File:Ch4e-ar-8.png|1000px]]&lt;br /&gt;
* The image below shows some of the methods generated by scaffolding in the controller for category.&lt;br /&gt;
[[File:Ch4e-ar-7.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Migrations===&lt;br /&gt;
* Migrations are ruby ways to alter the database in a structured and organized manner. &lt;br /&gt;
* Although, it is possible for a developer to write SQL statements and alter it, migrations make the changes noticeable to other developers as well.&lt;br /&gt;
* One of the major advantages of migrations is that they allow version control on the database schema [http://guides.rubyonrails.org/migrations.html]. &lt;br /&gt;
* When you move between different versions of your code, Active Record will work out which migrations should be run. It will also update the db/schema.rb file to match the structure of your database at that version.&lt;br /&gt;
* Another advantage of migrations is that they allow database portability. Use migrations is similar to using an interface, using SQL is similar to using an implementation. Hence, using migrations can allow you to use SQLite3 for development and say MySQL in production. The migrations will transform you schema into the database specific construct, without having you worry about it.&lt;br /&gt;
* To create a migration, go to &amp;quot;Tools&amp;quot; menu, then &amp;quot;Rake tasks&amp;quot;, then &amp;quot;db&amp;quot; and click on &amp;quot;migrate&amp;quot;.&lt;br /&gt;
[[File:Ch4e-ar-9.png|1000px]]&lt;br /&gt;
* Now, select which migration you want to run from the drop down menu.&lt;br /&gt;
[[File:Ch4e-ar-10.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
===Fixtures===&lt;br /&gt;
Fixtures are used to provide our tests with data i.e. sample data[http://ar.rubyonrails.org/classes/Fixtures.html]. They come in three flavors:&lt;br /&gt;
* YAML fixtures&lt;br /&gt;
* CSV fixtures&lt;br /&gt;
* Single-file fixtures&lt;br /&gt;
YAML is the default format. It is a cleaner version of XML for representing data. Each model has a corresponding fixture file with the same name in test/fixtures/  path of your app’s root directory. The fixture file ends with .yml extension. Each YAML fixture is given a name and is followed by an indented list of key/value pairs in “key:value” format.&lt;br /&gt;
 &lt;br /&gt;
In the above fig. one is a name of fixture. It does not matter what name you use. The entries after one are the “column_name:value”  for the record one.&lt;br /&gt;
You can use scaffold to generate the fixtures for you. But it’ll give just the basic features. If there are any relationships then you have to explicitly specify them. You can do it by adding just foreign key name and it’ll figure out from where to populate the foreign key[http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/]. For example if you have used scaffold to generate MVC components of categories and recipes then it’ll generate categories.yml and recipes.yml for you. But now you’ll need to add the relationships manually, as shown below&lt;br /&gt;
 &lt;br /&gt;
One thing you need to think is that for how long you want the data in test database. You need it just for testing and once it is finished you can throw it away. Also for the testing it does not make sense to read the data from a remote server or from a file because of the life span of a test. So instead it’s just handy to load the database into memory, run your tests and throw it away. Because when we run the tests the data gets corrupted anyway for the next sets of tests.&lt;br /&gt;
&lt;br /&gt;
Steps to make changes in your app for fixtures&lt;br /&gt;
* Go to gem file and (bundler uses the gem file) and add a gem “memory_test_fix” inside “group :test” as shown in fig below. It’s a monkey patch which is specially written to load the database and throw in the end. If we do just the “rake:db:migrate” task then it is not sufficient as it’ll bring the data in memory but as soon as the task finishes everything is gone away. So we’ll not be able to run our tests.  This patch will keep the data in memory during the iteration of our tests.&lt;br /&gt;
* Also add a gem “test_unit” to get a GUI to see the test results.&lt;br /&gt;
* Go to database.yml file (config/database.yml) and change the test database to “:memory:”. It’ll keep the test database in memory and never look at disk so our test will run fast. It is not required but will help when we are having so many tests to run.&lt;br /&gt;
* Update bundler as you have added new packages.&lt;br /&gt;
&lt;br /&gt;
Sometimes for testing the content of the data is not as much important as is the volume. In those cases, you can mix ERB in your fixtures. For example if you want 100 categories in your category table then instead of writing them manually you can do it as – &lt;br /&gt;
 &lt;br /&gt;
Now in your tests you can use fixtures by specifying which fixture you want. For example, if you want posts fixture in one of your tests then you can specify it as&lt;br /&gt;
&lt;br /&gt;
[[File:55.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Unit tests===&lt;br /&gt;
Unit tests are used for testing models. When we generate a model using the scaffold it also generates a unit test script for the model in the tests directory[http://guides.rubyonrails.org/testing.html]. In unit tests we can use the data generated using fixtures. As we write code in model classes we must write corresponding unit tests. &lt;br /&gt;
For example consider you want all the fields for Recipe to be present. It can be tested as shown below – &lt;br /&gt;
 &lt;br /&gt;
*assert – It evaluates an object (or expression) for expected results. &lt;br /&gt;
If you have used “test_unit” gem then on executing tests you will get a result screen as &lt;br /&gt;
[[File:UnitTests.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
The green bar means that all the tests have passed. If any of the tests fails then you’ll get a red var.&lt;br /&gt;
&lt;br /&gt;
===Functional Tests===&lt;br /&gt;
Functional testing is a type of black-box testing which verifies the system behavior according to the specifications[http://en.wikipedia.org/wiki/Functional_testing]. In a rails projects, we apply functional tests on controllers to validate the workflow that they setup and verify if it results in the intended outcome. In rails, the controllers are primarily responsible for tasks such as routing of requests, handling user authentication, validating correctness of output produced etc. Of three entities in a rails project, there is tight coupling between the controller and the view [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/]. Hence, as a part of functional testing we also validate if the the content displayed by the view is of the correct format and content and the layout of the page is as expected.&lt;br /&gt;
The image below shows some of the functional test written for the categories controller. As you can see, tests like &amp;quot;should create category&amp;quot; checks if the categories controller can function as per specifications i.e. if it can create a new category.&lt;br /&gt;
[[File:Functional.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
===Integration Tests===&lt;br /&gt;
Integration testing is a testing technique in which various software modules are combined and tested as a group[http://en.wikipedia.org/wiki/Integration_testing]. Although each of the modules might function correctly, it is not necessary that they will function correctly as a group. In a rails project, integration tests are most useful to verify the interactions between controllers. Integration testing generally occurs in the latter part of the project testing life cycle. Unit and functional tests are written along with development. Once each component has been completed, its functionality tested in isolation, integration tests take precedence. In rails, a generator can create a integration test skeleton. In the image below some integration test for our rails application:&lt;br /&gt;
&lt;br /&gt;
[[File:Integration.png|1000px]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* 1. [http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/ Intermediate Rails: Understanding Models, Views and Controllers]&lt;br /&gt;
* 2. [http://en.wikipedia.org/wiki/Model-View-Controller Model–view–controller]&lt;br /&gt;
* 3. [http://guides.rubyonrails.org/action_controller_overview.html Action Controller Overview]&lt;br /&gt;
* 4. [http://guides.rubyonrails.org/migrations.html Migrations]&lt;br /&gt;
* 5. [http://ar.rubyonrails.org/classes/Fixtures.html Class Fixtures]&lt;br /&gt;
* 6. [http://biodegradablegeek.com/2008/07/how-to-use-fixtures-to-populate-your-database-in-rails/ How to use fixtures to Populate your database in rails ]&lt;br /&gt;
* 7. [http://guides.rubyonrails.org/testing.html A Guide to Testing Rails Applications]&lt;br /&gt;
* 8. [http://en.wikipedia.org/wiki/Functional_testing Functional testing]&lt;br /&gt;
* 9. [http://avdi.org/devblog/2010/12/21/object-oriented-programming-comes-to-rails/Object Oriented Programming Comes to Rails]&lt;br /&gt;
* 10. [http://en.wikipedia.org/wiki/Integration_testing Integration Testing]&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:55.png&amp;diff=54108</id>
		<title>File:55.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:55.png&amp;diff=54108"/>
		<updated>2011-10-22T01:58:32Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:43.png&amp;diff=54107</id>
		<title>File:43.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:43.png&amp;diff=54107"/>
		<updated>2011-10-22T01:54:20Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2b_sa&amp;diff=51664</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2b_sa&amp;diff=51664"/>
		<updated>2011-10-01T19:17:51Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Access Control  in Object Oriented Languages'''&lt;br /&gt;
:Each Object Oriented Language has its own way of implementing Access Control .This page discusses the various ways in which Access control is implemented in some of the Object Oriented Languages. &lt;br /&gt;
=='''Access Control'''==&lt;br /&gt;
Access control in programming languages provides the programmer the ability to define standards that are needed to hide the implementation of the modules from the public interface.Access control provides the programmer ,the ability to prevent under privileged users from access to internal components of a program and restricts them from changing the data within the component to an inconsistent state(i.e elements(members functions and variables) within a program(class)) there by protecting the integrity of the components .This concept of hiding the information in object oriented languages is called [http://en.wikipedia.org/wiki/Encapsulation Encapsulation].&lt;br /&gt;
&lt;br /&gt;
==Access Control in Ruby==&lt;br /&gt;
Access control in ruby is achieved using the following [http://en.wikipedia.org/wiki/Java_syntax#Access_modifiers access modifiers]for the methods.&lt;br /&gt;
*''public''    :The methods that are declared as public can be accessed by any object belong to the class .&lt;br /&gt;
*''private''   :The methods that are declared as private can only be invoked in the context of the current object.&lt;br /&gt;
*''protected'' :The methods that are declared as protected can be invoked by the objects of both the class and the sub-classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby these modifiers only apply to the methods but not the instance variables.&lt;br /&gt;
&lt;br /&gt;
====Public Methods in Ruby====&lt;br /&gt;
Public methods in ruby can be accessed by any object or instance of a Class and its Sub-classes.&lt;br /&gt;
Declaration of  public methods:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;      &lt;br /&gt;
class A              &lt;br /&gt;
  def method     &lt;br /&gt;
    puts &amp;quot;hello&amp;quot;    &lt;br /&gt;
  end               &lt;br /&gt;
end          &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;      &lt;br /&gt;
class A              &lt;br /&gt;
  def method     &lt;br /&gt;
    puts &amp;quot;hello&amp;quot;    &lt;br /&gt;
  end &lt;br /&gt;
  public :method              &lt;br /&gt;
end               &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Private Methods in Ruby====&lt;br /&gt;
private methods in Ruby can only be invoked by the current object in question and one cannot invoke other object's private methods.&lt;br /&gt;
In Ruby private methods are not private to the class but they are private to the objects. No method is perfectly private in Ruby.&lt;br /&gt;
&lt;br /&gt;
======Declaration of Private methods======&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A &lt;br /&gt;
 private  &lt;br /&gt;
   def method&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A&lt;br /&gt;
   def method&lt;br /&gt;
   end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example to show that private methods in Ruby can only be invoked by the current object in context.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It gives an error because pubm method is called in the context of object 'a',so only object 'a' can &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        invoke the private method but not the new object. &lt;br /&gt;
  end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B&lt;br /&gt;
  def m1&lt;br /&gt;
     method                         //private methods of the base class can be accessed by the sub class&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
b=B.new&lt;br /&gt;
a.pubm&lt;br /&gt;
a.method                           //gives an error&lt;br /&gt;
b.m1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Protected Methods in Ruby===&lt;br /&gt;
protected methods are visible in both the base class as well as the sub-classes(i.e they can be invoked by the objects of both the class and its sub-classes).&lt;br /&gt;
&lt;br /&gt;
Declaration of protected methods is ruby.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
 protected&lt;br /&gt;
  def method&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
  end&lt;br /&gt;
 protected :method&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Unlike private methods ,In Ruby protected methods can be called with an explicit receiver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a protected method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It does not give an error because in the context of the current object, another object belonging to same &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        class can invoke the protected method .&lt;br /&gt;
  end&lt;br /&gt;
  protected :method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B&lt;br /&gt;
  def m1&lt;br /&gt;
     method                         //protected methods of the base class can be accessed by the sub class.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
b=B.new&lt;br /&gt;
a.pubm&lt;br /&gt;
a.method                           //gives an error&lt;br /&gt;
b.m1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby both the private as well as the protected methods can be accessed from outside the class using send method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def privateMethod&lt;br /&gt;
    puts &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    privateMethod&lt;br /&gt;
    A.new.method                     // It does not give an error because in the context of the current object, another object belonging to same &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        class can invoke the protected method .&lt;br /&gt;
  end&lt;br /&gt;
  private :privateMethod&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
a.method                       // gives an error&lt;br /&gt;
a.send(:privateMethod)                // prints &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
a.send :privateMethod                 // prints &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
a.method(:privateMethod).call  // prints &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Access Control in C++==&lt;br /&gt;
&lt;br /&gt;
===Controlling Access to the Member Data and Member Functions===&lt;br /&gt;
&lt;br /&gt;
In C++ one can restrict the level of access control on the member data a&lt;br /&gt;
nd function using the access specifiers public,private and protected.&lt;br /&gt;
A constructor should never be declared as private.&lt;br /&gt;
Access control is applied to all the names uniformly.&lt;br /&gt;
&lt;br /&gt;
===Controlling the Level of accessibility of Base class and its Members===&lt;br /&gt;
If a class is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) inherited]using a public access specifier than the public members of the base class are inherited as the public members of the derived class and  protected members of the base class are inherited as the protected members of the derived class.If a class is inherited using a private access specifier than both the public and protected members of the base class become the private members of the derived class.If a class is inherited using a protected access specifier than both the public and protected members of the base class become the protected. members of the derived class.&lt;br /&gt;
[[File:Access.png|400px|center|upright=0.56|Diagram Depicting the Visibility of members and the Inheritance effect.]]&lt;br /&gt;
However, the sub-class members can determine the values of the  base class private members by using the Base class public member functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
      int a;&lt;br /&gt;
      public:&lt;br /&gt;
             int value()&lt;br /&gt;
             {&lt;br /&gt;
                 a=10;&lt;br /&gt;
                 return a;&lt;br /&gt;
             }&lt;br /&gt;
};&lt;br /&gt;
class B:public A&lt;br /&gt;
{&lt;br /&gt;
      public:     &lt;br /&gt;
             int b;&lt;br /&gt;
             void bvalue()&lt;br /&gt;
             {&lt;br /&gt;
                 b=value();  //now b is assigned a value 10 .This is how the sub class can determine the value of the base class private member &lt;br /&gt;
                 printf(&amp;quot;%d&amp;quot;,b);  variables        &lt;br /&gt;
             }&lt;br /&gt;
};&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
     B o;&lt;br /&gt;
     o.bvalue();&lt;br /&gt;
     return 1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding] can be used to access the members of the derived class using the Base class pointer.Dynamic binding or late-binding or run time binding is achieved using virtual functions in the Base class.If the Base class pointer pointing to the derived class object and the matching function is declared virtual in base class, then which function is call is decided at run-time using virtual table entry.&lt;br /&gt;
 &lt;br /&gt;
===Friendly Functions===&lt;br /&gt;
Like ''send'' method in Ruby,C++ allow the Friend Functions which are not within the scope of the class to access the private members of the class. The friend function are defined like any other function without using either the keyword friend or [http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7l.doc%2Flanguage%2Fref%2Fclrc05cplr175.htm :: scope resolution operator].Since they are not within the scope of the class ,they cannot be called using the objects of the class.Declaration of a friend function in either the public or private part of the class does not change it functionality.&lt;br /&gt;
&lt;br /&gt;
===Friend Classes===&lt;br /&gt;
A class declared as a [http://en.wikipedia.org/wiki/Friend_class friend class] of an another class can access all the private and protected members of the class extending friendship.&lt;br /&gt;
&lt;br /&gt;
==Access Control in Java==&lt;br /&gt;
Access control can be defined as the methods that limit accessibility of the resources to a set of users or programs to enforce integrity, confidentiality or availability constraints. &lt;br /&gt;
Java provides two levels of access controls&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Top Level&amp;quot; – Public, or Package-Private (no explicit modifier, also known as default).&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Member Level&amp;quot; – Public, Private, Protected or Package-Private (no explicit modifier, also known as default).&lt;br /&gt;
===Top Level===&lt;br /&gt;
On the Top level come classes and interfaces. An Interface can only be declared as public. While a class can be declared to be either public or default. If a class is declared as public then it is visible to all the classes in the same or a different package. But if a class is declared with no access specifier i.e. default, then it is public in its own package i.e. it cannot be accessed outside the package other than the one in which it is declared.&lt;br /&gt;
===Member Level===&lt;br /&gt;
At the member level we can declare fields, methods and constructors as public, private, protected and default. Public and default modifiers are used in the same way as the top level and with the same meaning. When a member of a class is declared as private then it can be accessed only within the class. Also, when a member is declared as protected then it can be accessed by all the classes in the same package and the sub classes (of its class) in other packages.&lt;br /&gt;
===Final Keyword===&lt;br /&gt;
When this keyword is used along with the class declaration, the class cannot be inherited by any other class.If a method is declared to be final then that method cannot be overridden and if a variable is declared as final then its value cannot be changed.&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
To understand it in more detail consider the following the example.&lt;br /&gt;
[[File:Access1.png|400px|center]]&lt;br /&gt;
In the example each package contains two classes. Class C is a sub class of class A. Assuming class A as public then its members can be accessed like –&lt;br /&gt;
Class A – It can access all its members independent of their access modifier.&lt;br /&gt;
Class B – It can access all the members of class A except those are private. &lt;br /&gt;
Class C – It can access public and protected members of class A because it is a sub class of class A.&lt;br /&gt;
Class D – It can access only the public members of class A. &lt;br /&gt;
The access modifiers of the top level component govern whether its members are available in the other packages i.e. even if we are having public methods in a class but the class is declared as default then its methods will not be available in other packages. &lt;br /&gt;
If one’s class is going to be used by other coders then he must ensure that errors due to misuse do not happen. For this he must provide the most restrictive access to its members like always declare the fields as private and provide methods to access/modify them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package package1;&lt;br /&gt;
&lt;br /&gt;
public class Parent {&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * &lt;br /&gt;
	 */&lt;br /&gt;
	private int privateVariable;&lt;br /&gt;
	public int publicVariable;&lt;br /&gt;
	protected int protectedVariable;&lt;br /&gt;
	int defaultVariable;&lt;br /&gt;
	&lt;br /&gt;
	private int getPrivateVariable() {&lt;br /&gt;
		return privateVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	private void setPrivateVariable(int privateVariable) {&lt;br /&gt;
		this.privateVariable = privateVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public int getPublicVariable() {&lt;br /&gt;
		return publicVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public void setPublicVariable(int publicVariable) {&lt;br /&gt;
		this.publicVariable = publicVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	protected int getProtectedVariable() {&lt;br /&gt;
		return protectedVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	protected void setProtectedVariable(int protectedVariable) {&lt;br /&gt;
		this.protectedVariable = protectedVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	int getDefaultVariable() {&lt;br /&gt;
		return defaultVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	void setDefaultVariable(int defaultVariable) {&lt;br /&gt;
		this.defaultVariable = defaultVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		// TODO Auto-generated method stub&lt;br /&gt;
		Parent parent=new Parent();&lt;br /&gt;
		parent.setDefaultVariable(5);&lt;br /&gt;
		parent.setPrivateVariable(4);&lt;br /&gt;
		parent.setProtectedVariable(6);&lt;br /&gt;
		parent.setPublicVariable(9);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
package package1;&lt;br /&gt;
&lt;br /&gt;
public class ChildInSamePackage extends Parent{&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Public, Protected and Default members available.&lt;br /&gt;
	 */&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		// TODO Auto-generated method stub&lt;br /&gt;
		ChildInSamePackage childInSamePackage = new ChildInSamePackage();&lt;br /&gt;
		childInSamePackage.setDefaultVariable(1);&lt;br /&gt;
		childInSamePackage.setProtectedVariable(5);&lt;br /&gt;
		childInSamePackage.setPublicVariable(7);&lt;br /&gt;
		//childInSamePackage.setPrivateVariable(7); // not allowed. Compile time Error&lt;br /&gt;
		&lt;br /&gt;
		Parent parent =new Parent();&lt;br /&gt;
		parent.setDefaultVariable(0);&lt;br /&gt;
		parent.setProtectedVariable(1);&lt;br /&gt;
		parent.setPublicVariable(0);&lt;br /&gt;
		//parent.setPrivateVariable(0); //not allowed. compile time error&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
package package1;&lt;br /&gt;
&lt;br /&gt;
public class ClassInSamePackage {&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Public, Protected and Default available but only to parent class object.&lt;br /&gt;
	 */&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		// TODO Auto-generated method stub&lt;br /&gt;
		Parent parent = new Parent();&lt;br /&gt;
		parent.setDefaultVariable(0);&lt;br /&gt;
		parent.setProtectedVariable(5);&lt;br /&gt;
		parent.setPublicVariable(9);&lt;br /&gt;
		//parent.setPrivateVariable(9); //not allowed&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
package package2;&lt;br /&gt;
&lt;br /&gt;
import package1.Parent;&lt;br /&gt;
&lt;br /&gt;
public class ChildInDifferentPackage extends Parent{&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Both public and Protected members can be accessed. &lt;br /&gt;
	 * But Protected can be accessed only through inheritance.&lt;br /&gt;
	 */&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		// TODO Auto-generated method stub&lt;br /&gt;
		ChildInDifferentPackage childInDifferentPackage = new ChildInDifferentPackage();&lt;br /&gt;
		childInDifferentPackage.setProtectedVariable(5);&lt;br /&gt;
		childInDifferentPackage.setPublicVariable(4);&lt;br /&gt;
		&lt;br /&gt;
		Parent parent = new Parent();&lt;br /&gt;
		parent.setPublicVariable(7);&lt;br /&gt;
		//parent.setProtectedVariable(7); // not allowed. protected members are available only through inheritance.&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
package package2;&lt;br /&gt;
&lt;br /&gt;
import package1.Parent;&lt;br /&gt;
&lt;br /&gt;
public class ClassInDifferentPackage {&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Only public members of parent class can be accessed &lt;br /&gt;
	 * but using the object of parent class.  &lt;br /&gt;
	 */&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
				&lt;br /&gt;
		Parent parent = new Parent();&lt;br /&gt;
		parent.setPublicVariable(4); // only public members are available.&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Access Control in C#==&lt;br /&gt;
The accessibility levels of the types (class, interface, struct, enum etc.) and the members, controls whether they can be used from the other code in our assembly (*) or other assemblies.&lt;br /&gt;
&lt;br /&gt;
===Accessibility levels provided by C#===&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Public&amp;quot;: It can be used for the types and type members. It allows them to be accessed everywhere in the same assembly or the other assembly.&lt;br /&gt;
*&amp;quot;Private&amp;quot;: It can be used only for the members. Private members are accessible only within the class or the struct in which they are declared. &lt;br /&gt;
*&amp;quot;Protected&amp;quot;: It is a member access modifier. It allows the class to hide its members from the other classes except the child classes. &lt;br /&gt;
*&amp;quot;Internal&amp;quot;: It can be used both for the types and type members. Internal types and members are accessible only by the code in the same assembly.&lt;br /&gt;
*&amp;quot;Protected Internal&amp;quot;: It’s a combination of protected and internal modifier. It allows the types and the members to be used by any code in the same assembly or by any derived class in another assembly. A protected internal element can be accessed in a derived class in another assembly by using an instance of the derived class.&lt;br /&gt;
[[File:Access2.png|400px|center|upright=0.56]]&lt;br /&gt;
&lt;br /&gt;
Usage of these access modifiers for all types or members is dependent on the conditions like the accessibility of the container restricts the accessibility of a type member. &lt;br /&gt;
*- Assembly is a physical unit that can be executed, deployed, versioned and secured.&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                                &lt;br /&gt;
    internal class InternalClass&lt;br /&gt;
    {&lt;br /&gt;
        private int privateMember1;&lt;br /&gt;
        protected int protectedMember2;&lt;br /&gt;
        internal int internalMember3;&lt;br /&gt;
        protected internal int protectedInternalMember4;&lt;br /&gt;
&lt;br /&gt;
        private class NestedClass&lt;br /&gt;
        {&lt;br /&gt;
            void Test(Class1 foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.Member1 = 0; //OK&lt;br /&gt;
                foo.Member2 = 0; //OK&lt;br /&gt;
                foo.Member3 = 0; //OK&lt;br /&gt;
                foo.Member4 = 0; //OK&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public class PublicClass&lt;br /&gt;
    {&lt;br /&gt;
        private void Method1()&lt;br /&gt;
        {&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        private int privateMember1;&lt;br /&gt;
&lt;br /&gt;
        protected int Member1&lt;br /&gt;
        {&lt;br /&gt;
            get&lt;br /&gt;
            {&lt;br /&gt;
                return privateMember1;&lt;br /&gt;
            }&lt;br /&gt;
            set&lt;br /&gt;
            {&lt;br /&gt;
                Method1();&lt;br /&gt;
                privateMember1 = value;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        internal int internalMember2;&lt;br /&gt;
        public int publicMember3;&lt;br /&gt;
        protected internal int protectedInternalMember4;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class Test&lt;br /&gt;
    {&lt;br /&gt;
        void Test1()&lt;br /&gt;
        {&lt;br /&gt;
            InternalClass internalClass = new InternalClass(); //OK&lt;br /&gt;
            internalClass.privateMember1 = 0; //Compilation error here&lt;br /&gt;
            internalClass.protectedMember2 = 0; //Compilation error here&lt;br /&gt;
            internalClass.internalMember3 = 0; //OK&lt;br /&gt;
            internalClass.protectedInternalMember4 = 0; //OK&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    internal class DerivedClass : InternalClass&lt;br /&gt;
    {&lt;br /&gt;
        private void Test2()&lt;br /&gt;
        {&lt;br /&gt;
            privateMember1 = 0; //Compilation error here&lt;br /&gt;
            protectedMember2 = 0; //OK&lt;br /&gt;
            internalMember3 = 0; //OK&lt;br /&gt;
            protectedInternalMember4 = 0; //OK&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        private class NestedClass&lt;br /&gt;
        {&lt;br /&gt;
            void Test(DerivedClass foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.privateMember1 = 0; //Compilation error here&lt;br /&gt;
                foo.protectedMember2 = 0; //OK&lt;br /&gt;
                foo.internalMember3 = 0; //OK&lt;br /&gt;
                foo.protectedInternalMember4 = 0; //OK&lt;br /&gt;
            }&lt;br /&gt;
            void Test(Class1 foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.privateMember1 = 0; //Compilation error here&lt;br /&gt;
                foo.protectedMember2 = 0; //Compilation error here&lt;br /&gt;
                foo.internalMember3 = 0; //OK&lt;br /&gt;
                foo.protectedInternalMember4 = 0; //OK&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
------------------    Class in a different assembly     -------------------------------&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Test&lt;br /&gt;
    {&lt;br /&gt;
        void Test1()&lt;br /&gt;
        {&lt;br /&gt;
            InternalClass internalClass = new InternalClass(); //Compilation error here&lt;br /&gt;
&lt;br /&gt;
            PublicClass publicClass = new PublicClass; //OK&lt;br /&gt;
            publicClass.Method1(); //Compilation error here&lt;br /&gt;
            publicClass.privateMember1 = 0; //Compilation error here&lt;br /&gt;
            publicClass.Member1 = 0; //Compilation error here&lt;br /&gt;
            publicClass.internalMember2 = 0; //Compilation error here&lt;br /&gt;
            publicClass.publicMember3 = 0; //OK&lt;br /&gt;
            publicClass.protectedInternalMember4 = 0; //Compilation error here&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    internal class DerivedClass : PublicClass&lt;br /&gt;
    {&lt;br /&gt;
        private void Test2()&lt;br /&gt;
        {&lt;br /&gt;
            Member1 = 0; //OK&lt;br /&gt;
            internalMember2 = 0; //Compilation error here&lt;br /&gt;
            publicMember3 = 0; //OK&lt;br /&gt;
            protectedInternalMember4 = 0; //OK&lt;br /&gt;
        }&lt;br /&gt;
        private class NestedClass&lt;br /&gt;
        {&lt;br /&gt;
            void Test(DerivedClass foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.Member1 = 0; //Compilation error here&lt;br /&gt;
                foo.internalMember2 = 0; //Compilation error here&lt;br /&gt;
                foo.publicMember3 = 0; //OK&lt;br /&gt;
                foo.protectedInternalMember4 = 0; //OK&lt;br /&gt;
            }&lt;br /&gt;
            void Test(Class2 foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.Member1 = 0; //Compilation error here&lt;br /&gt;
                foo.internalMember2 = 0; //Compilation error here&lt;br /&gt;
                foo.publicMember3 = 0; //OK&lt;br /&gt;
                foo.protectedInternalMember4 = 0; //Compilation error here&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Access controls is one of the important features provided by the Object Oriented Languages as it helps in achieving Encapsulation.Thus one can make his code less vulnerable from being exploited by others utilizing the features and capabilities provided by the access specifiers . &lt;br /&gt;
The philosophies of access controls in object oriented languages have not changed much over time but certainly the access control across different Object Oriented Languages are different if not completely.&lt;br /&gt;
 &lt;br /&gt;
==References==&lt;br /&gt;
# http://ruby-doc.org/core/&lt;br /&gt;
# http://rubylearning.com/satishtalim/ruby_access_control.html&lt;br /&gt;
# http://phrogz.net/programmingruby/tut_classes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/kktasw36(v=VS.80).aspx&lt;br /&gt;
# http://www.kuzbass.ru:8086/docs/isocpp/access.html&lt;br /&gt;
# http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr130.htm&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx&lt;br /&gt;
# http://stackoverflow.com/questions/2084801/c-using-declaration-scope-and-access-control&lt;br /&gt;
# http://en.wikipedia.org/wiki/Class_(computer_programming)#Member_accessibility&lt;br /&gt;
# http://www.jvoegele.com/software/langcomp.html&lt;br /&gt;
# http://csharp.net-informations.com/language/csharp-access-specifiers.htm&lt;br /&gt;
# http://blogs.oberon.ch/tamberg/2007-10-30/understanding-csharp-access-modifiers.html&lt;br /&gt;
# http://www.enscand.com/howtos/access/access.html&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2b_sa&amp;diff=51663</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2b_sa&amp;diff=51663"/>
		<updated>2011-10-01T19:16:21Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Access Control  in Object Oriented Languages'''&lt;br /&gt;
:Each Object Oriented Language has its own way of implementing Access Control .This page discusses the various ways in which Access control is implemented in some of the Object Oriented Languages. &lt;br /&gt;
=='''Access Control'''==&lt;br /&gt;
Access control in programming languages provides the programmer the ability to define standards that are needed to hide the implementation of the modules from the public interface.Access control provides the programmer ,the ability to prevent under privileged users from access to internal components of a program and restricts them from changing the data within the component to an inconsistent state(i.e elements(members functions and variables) within a program(class)) there by protecting the integrity of the components .This concept of hiding the information in object oriented languages is called [http://en.wikipedia.org/wiki/Encapsulation Encapsulation].&lt;br /&gt;
&lt;br /&gt;
==Access Control in Ruby==&lt;br /&gt;
Access control in ruby is achieved using the following [http://en.wikipedia.org/wiki/Java_syntax#Access_modifiers access modifiers]for the methods.&lt;br /&gt;
*''public''    :The methods that are declared as public can be accessed by any object belong to the class .&lt;br /&gt;
*''private''   :The methods that are declared as private can only be invoked in the context of the current object.&lt;br /&gt;
*''protected'' :The methods that are declared as protected can be invoked by the objects of both the class and the sub-classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby these modifiers only apply to the methods but not the instance variables.&lt;br /&gt;
&lt;br /&gt;
====Public Methods in Ruby====&lt;br /&gt;
Public methods in ruby can be accessed by any object or instance of a Class and its Sub-classes.&lt;br /&gt;
Declaration of  public methods:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;      &lt;br /&gt;
class A              &lt;br /&gt;
  def method     &lt;br /&gt;
    puts &amp;quot;hello&amp;quot;    &lt;br /&gt;
  end               &lt;br /&gt;
end          &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;      &lt;br /&gt;
class A              &lt;br /&gt;
  def method     &lt;br /&gt;
    puts &amp;quot;hello&amp;quot;    &lt;br /&gt;
  end &lt;br /&gt;
  public :method              &lt;br /&gt;
end               &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Private Methods in Ruby====&lt;br /&gt;
private methods in Ruby can only be invoked by the current object in question and one cannot invoke other object's private methods.&lt;br /&gt;
In Ruby private methods are not private to the class but they are private to the objects. No method is perfectly private in Ruby.&lt;br /&gt;
&lt;br /&gt;
======Declaration of Private methods======&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A &lt;br /&gt;
 private  &lt;br /&gt;
   def method&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A&lt;br /&gt;
   def method&lt;br /&gt;
   end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example to show that private methods in Ruby can only be invoked by the current object in context.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It gives an error because pubm method is called in the context of object 'a',so only object 'a' can &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        invoke the private method but not the new object. &lt;br /&gt;
  end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B&lt;br /&gt;
  def m1&lt;br /&gt;
     method                         //private methods of the base class can be accessed by the sub class&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
b=B.new&lt;br /&gt;
a.pubm&lt;br /&gt;
a.method                           //gives an error&lt;br /&gt;
b.m1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Protected Methods in Ruby===&lt;br /&gt;
protected methods are visible in both the base class as well as the sub-classes(i.e they can be invoked by the objects of both the class and its sub-classes).&lt;br /&gt;
&lt;br /&gt;
Declaration of protected methods is ruby.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
 protected&lt;br /&gt;
  def method&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
  end&lt;br /&gt;
 protected :method&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Unlike private methods ,In Ruby protected methods can be called with an explicit receiver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a protected method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It does not give an error because in the context of the current object, another object belonging to same &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        class can invoke the protected method .&lt;br /&gt;
  end&lt;br /&gt;
  protected :method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B&lt;br /&gt;
  def m1&lt;br /&gt;
     method                         //protected methods of the base class can be accessed by the sub class.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
b=B.new&lt;br /&gt;
a.pubm&lt;br /&gt;
a.method                           //gives an error&lt;br /&gt;
b.m1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby both the private as well as the protected methods can be accessed from outside the class using send method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def privateMethod&lt;br /&gt;
    puts &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    privateMethod&lt;br /&gt;
    A.new.method                     // It does not give an error because in the context of the current object, another object belonging to same &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        class can invoke the protected method .&lt;br /&gt;
  end&lt;br /&gt;
  private :privateMethod&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
a.method                       // gives an error&lt;br /&gt;
a.send(:privateMethod)                // prints &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
a.send :privateMethod                 // prints &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
a.method(:privateMethod).call  // prints &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Access Control in C++==&lt;br /&gt;
&lt;br /&gt;
===Controlling Access to the Member Data and Member Functions===&lt;br /&gt;
&lt;br /&gt;
In C++ one can restrict the level of access control on the member data a&lt;br /&gt;
nd function using the access specifiers public,private and protected.&lt;br /&gt;
A constructor should never be declared as private.&lt;br /&gt;
Access control is applied to all the names uniformly.&lt;br /&gt;
&lt;br /&gt;
===Controlling the Level of accessibility of Base class and its Members===&lt;br /&gt;
If a class is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) inherited]using a public access specifier than the public members of the base class are inherited as the public members of the derived class and  protected members of the base class are inherited as the protected members of the derived class.If a class is inherited using a private access specifier than both the public and protected members of the base class become the private members of the derived class.If a class is inherited using a protected access specifier than both the public and protected members of the base class become the protected. members of the derived class.&lt;br /&gt;
[[File:Access.png|400px|center|upright=0.56|Diagram Depicting the Visibility of members and the Inheritance effect.]]&lt;br /&gt;
However, the sub-class members can determine the values of the  base class private members by using the Base class public member functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
      int a;&lt;br /&gt;
      public:&lt;br /&gt;
             int value()&lt;br /&gt;
             {&lt;br /&gt;
                 a=10;&lt;br /&gt;
                 return a;&lt;br /&gt;
             }&lt;br /&gt;
};&lt;br /&gt;
class B:public A&lt;br /&gt;
{&lt;br /&gt;
      public:     &lt;br /&gt;
             int b;&lt;br /&gt;
             void bvalue()&lt;br /&gt;
             {&lt;br /&gt;
                 b=value();  //now b is assigned a value 10 .This is how the sub class can determine the value of the base class private member &lt;br /&gt;
                 printf(&amp;quot;%d&amp;quot;,b);  variables        &lt;br /&gt;
             }&lt;br /&gt;
};&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
     B o;&lt;br /&gt;
     o.bvalue();&lt;br /&gt;
     return 1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding] can be used to access the members of the derived class using the Base class pointer.Dynamic binding or late-binding or run time binding is achieved using virtual functions in the Base class.If the Base class pointer pointing to the derived class object and the matching function is declared virtual in base class, then which function is call is decided at run-time using virtual table entry.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Friendly Functions===&lt;br /&gt;
Like ''send'' method in Ruby,C++ allow the Friend Functions which are not within the scope of the class to access the private members of the class. The friend function are defined like any other function without using either the keyword friend or [http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7l.doc%2Flanguage%2Fref%2Fclrc05cplr175.htm :: scope resolution operator].Since they are not within the scope of the class ,they cannot be called using the objects of the class.Declaration of a friend function in either the public or private part of the class does not change it functionality.&lt;br /&gt;
&lt;br /&gt;
===Friend Classes===&lt;br /&gt;
A class declared as a [http://en.wikipedia.org/wiki/Friend_class friend class] of an another class can access all the private and protected members of the class extending friendship.&lt;br /&gt;
&lt;br /&gt;
==Access Control in Java==&lt;br /&gt;
Access control can be defined as the methods that limit accessibility of the resources to a set of users or programs to enforce integrity, confidentiality or availability constraints. &lt;br /&gt;
Java provides two levels of access controls&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Top Level&amp;quot; – Public, or Package-Private (no explicit modifier, also known as default).&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Member Level&amp;quot; – Public, Private, Protected or Package-Private (no explicit modifier, also known as default).&lt;br /&gt;
===Top Level===&lt;br /&gt;
On the Top level come classes and interfaces. An Interface can only be declared as public. While a class can be declared to be either public or default. If a class is declared as public then it is visible to all the classes in the same or a different package. But if a class is declared with no access specifier i.e. default, then it is public in its own package i.e. it cannot be accessed outside the package other than the one in which it is declared.&lt;br /&gt;
===Member Level===&lt;br /&gt;
At the member level we can declare fields, methods and constructors as public, private, protected and default. Public and default modifiers are used in the same way as the top level and with the same meaning. When a member of a class is declared as private then it can be accessed only within the class. Also, when a member is declared as protected then it can be accessed by all the classes in the same package and the sub classes (of its class) in other packages.&lt;br /&gt;
===Final Keyword===&lt;br /&gt;
&lt;br /&gt;
When this keyword is used along with the class declaration, the class cannot be inherited by any other class.If a method is declared to be final then that method cannot be overridden and if a variable is declared as final then its value cannot be changed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
To understand it in more detail consider the following the example.&lt;br /&gt;
[[File:Access1.png|400px|center]]&lt;br /&gt;
In the example each package contains two classes. Class C is a sub class of class A. Assuming class A as public then its members can be accessed like –&lt;br /&gt;
Class A – It can access all its members independent of their access modifier.&lt;br /&gt;
Class B – It can access all the members of class A except those are private. &lt;br /&gt;
Class C – It can access public and protected members of class A because it is a sub class of class A.&lt;br /&gt;
Class D – It can access only the public members of class A. &lt;br /&gt;
The access modifiers of the top level component govern whether its members are available in the other packages i.e. even if we are having public methods in a class but the class is declared as default then its methods will not be available in other packages. &lt;br /&gt;
If one’s class is going to be used by other coders then he must ensure that errors due to misuse do not happen. For this he must provide the most restrictive access to its members like always declare the fields as private and provide methods to access/modify them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package package1;&lt;br /&gt;
&lt;br /&gt;
public class Parent {&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * &lt;br /&gt;
	 */&lt;br /&gt;
	private int privateVariable;&lt;br /&gt;
	public int publicVariable;&lt;br /&gt;
	protected int protectedVariable;&lt;br /&gt;
	int defaultVariable;&lt;br /&gt;
	&lt;br /&gt;
	private int getPrivateVariable() {&lt;br /&gt;
		return privateVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	private void setPrivateVariable(int privateVariable) {&lt;br /&gt;
		this.privateVariable = privateVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public int getPublicVariable() {&lt;br /&gt;
		return publicVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public void setPublicVariable(int publicVariable) {&lt;br /&gt;
		this.publicVariable = publicVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	protected int getProtectedVariable() {&lt;br /&gt;
		return protectedVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	protected void setProtectedVariable(int protectedVariable) {&lt;br /&gt;
		this.protectedVariable = protectedVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	int getDefaultVariable() {&lt;br /&gt;
		return defaultVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	void setDefaultVariable(int defaultVariable) {&lt;br /&gt;
		this.defaultVariable = defaultVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		// TODO Auto-generated method stub&lt;br /&gt;
		Parent parent=new Parent();&lt;br /&gt;
		parent.setDefaultVariable(5);&lt;br /&gt;
		parent.setPrivateVariable(4);&lt;br /&gt;
		parent.setProtectedVariable(6);&lt;br /&gt;
		parent.setPublicVariable(9);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
package package1;&lt;br /&gt;
&lt;br /&gt;
public class ChildInSamePackage extends Parent{&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Public, Protected and Default members available.&lt;br /&gt;
	 */&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		// TODO Auto-generated method stub&lt;br /&gt;
		ChildInSamePackage childInSamePackage = new ChildInSamePackage();&lt;br /&gt;
		childInSamePackage.setDefaultVariable(1);&lt;br /&gt;
		childInSamePackage.setProtectedVariable(5);&lt;br /&gt;
		childInSamePackage.setPublicVariable(7);&lt;br /&gt;
		//childInSamePackage.setPrivateVariable(7); // not allowed. Compile time Error&lt;br /&gt;
		&lt;br /&gt;
		Parent parent =new Parent();&lt;br /&gt;
		parent.setDefaultVariable(0);&lt;br /&gt;
		parent.setProtectedVariable(1);&lt;br /&gt;
		parent.setPublicVariable(0);&lt;br /&gt;
		//parent.setPrivateVariable(0); //not allowed. compile time error&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
package package1;&lt;br /&gt;
&lt;br /&gt;
public class ClassInSamePackage {&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Public, Protected and Default available but only to parent class object.&lt;br /&gt;
	 */&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		// TODO Auto-generated method stub&lt;br /&gt;
		Parent parent = new Parent();&lt;br /&gt;
		parent.setDefaultVariable(0);&lt;br /&gt;
		parent.setProtectedVariable(5);&lt;br /&gt;
		parent.setPublicVariable(9);&lt;br /&gt;
		//parent.setPrivateVariable(9); //not allowed&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
package package2;&lt;br /&gt;
&lt;br /&gt;
import package1.Parent;&lt;br /&gt;
&lt;br /&gt;
public class ChildInDifferentPackage extends Parent{&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Both public and Protected members can be accessed. &lt;br /&gt;
	 * But Protected can be accessed only through inheritance.&lt;br /&gt;
	 */&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		// TODO Auto-generated method stub&lt;br /&gt;
		ChildInDifferentPackage childInDifferentPackage = new ChildInDifferentPackage();&lt;br /&gt;
		childInDifferentPackage.setProtectedVariable(5);&lt;br /&gt;
		childInDifferentPackage.setPublicVariable(4);&lt;br /&gt;
		&lt;br /&gt;
		Parent parent = new Parent();&lt;br /&gt;
		parent.setPublicVariable(7);&lt;br /&gt;
		//parent.setProtectedVariable(7); // not allowed. protected members are available only through inheritance.&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
package package2;&lt;br /&gt;
&lt;br /&gt;
import package1.Parent;&lt;br /&gt;
&lt;br /&gt;
public class ClassInDifferentPackage {&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Only public members of parent class can be accessed &lt;br /&gt;
	 * but using the object of parent class.  &lt;br /&gt;
	 */&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
				&lt;br /&gt;
		Parent parent = new Parent();&lt;br /&gt;
		parent.setPublicVariable(4); // only public members are available.&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Access Control in C#==&lt;br /&gt;
The accessibility levels of the types (class, interface, struct, enum etc.) and the members, controls whether they can be used from the other code in our assembly (*) or other assemblies.&lt;br /&gt;
&lt;br /&gt;
===Accessibility levels provided by C#===&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Public&amp;quot;: It can be used for the types and type members. It allows them to be accessed everywhere in the same assembly or the other assembly.&lt;br /&gt;
*&amp;quot;Private&amp;quot;: It can be used only for the members. Private members are accessible only within the class or the struct in which they are declared. &lt;br /&gt;
*&amp;quot;Protected&amp;quot;: It is a member access modifier. It allows the class to hide its members from the other classes except the child classes. &lt;br /&gt;
*&amp;quot;Internal&amp;quot;: It can be used both for the types and type members. Internal types and members are accessible only by the code in the same assembly.&lt;br /&gt;
*&amp;quot;Protected Internal&amp;quot;: It’s a combination of protected and internal modifier. It allows the types and the members to be used by any code in the same assembly or by any derived class in another assembly. A protected internal element can be accessed in a derived class in another assembly by using an instance of the derived class.&lt;br /&gt;
[[File:Access2.png|400px|center|upright=0.56]]&lt;br /&gt;
&lt;br /&gt;
Usage of these access modifiers for all types or members is dependent on the conditions like the accessibility of the container restricts the accessibility of a type member. &lt;br /&gt;
*- Assembly is a physical unit that can be executed, deployed, versioned and secured.&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                                &lt;br /&gt;
    internal class InternalClass&lt;br /&gt;
    {&lt;br /&gt;
        private int privateMember1;&lt;br /&gt;
        protected int protectedMember2;&lt;br /&gt;
        internal int internalMember3;&lt;br /&gt;
        protected internal int protectedInternalMember4;&lt;br /&gt;
&lt;br /&gt;
        private class NestedClass&lt;br /&gt;
        {&lt;br /&gt;
            void Test(Class1 foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.Member1 = 0; //OK&lt;br /&gt;
                foo.Member2 = 0; //OK&lt;br /&gt;
                foo.Member3 = 0; //OK&lt;br /&gt;
                foo.Member4 = 0; //OK&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public class PublicClass&lt;br /&gt;
    {&lt;br /&gt;
        private void Method1()&lt;br /&gt;
        {&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        private int privateMember1;&lt;br /&gt;
&lt;br /&gt;
        protected int Member1&lt;br /&gt;
        {&lt;br /&gt;
            get&lt;br /&gt;
            {&lt;br /&gt;
                return privateMember1;&lt;br /&gt;
            }&lt;br /&gt;
            set&lt;br /&gt;
            {&lt;br /&gt;
                Method1();&lt;br /&gt;
                privateMember1 = value;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        internal int internalMember2;&lt;br /&gt;
        public int publicMember3;&lt;br /&gt;
        protected internal int protectedInternalMember4;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class Test&lt;br /&gt;
    {&lt;br /&gt;
        void Test1()&lt;br /&gt;
        {&lt;br /&gt;
            InternalClass internalClass = new InternalClass(); //OK&lt;br /&gt;
            internalClass.privateMember1 = 0; //Compilation error here&lt;br /&gt;
            internalClass.protectedMember2 = 0; //Compilation error here&lt;br /&gt;
            internalClass.internalMember3 = 0; //OK&lt;br /&gt;
            internalClass.protectedInternalMember4 = 0; //OK&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    internal class DerivedClass : InternalClass&lt;br /&gt;
    {&lt;br /&gt;
        private void Test2()&lt;br /&gt;
        {&lt;br /&gt;
            privateMember1 = 0; //Compilation error here&lt;br /&gt;
            protectedMember2 = 0; //OK&lt;br /&gt;
            internalMember3 = 0; //OK&lt;br /&gt;
            protectedInternalMember4 = 0; //OK&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        private class NestedClass&lt;br /&gt;
        {&lt;br /&gt;
            void Test(DerivedClass foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.privateMember1 = 0; //Compilation error here&lt;br /&gt;
                foo.protectedMember2 = 0; //OK&lt;br /&gt;
                foo.internalMember3 = 0; //OK&lt;br /&gt;
                foo.protectedInternalMember4 = 0; //OK&lt;br /&gt;
            }&lt;br /&gt;
            void Test(Class1 foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.privateMember1 = 0; //Compilation error here&lt;br /&gt;
                foo.protectedMember2 = 0; //Compilation error here&lt;br /&gt;
                foo.internalMember3 = 0; //OK&lt;br /&gt;
                foo.protectedInternalMember4 = 0; //OK&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
------------------    Class in a different assembly     -------------------------------&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Test&lt;br /&gt;
    {&lt;br /&gt;
        void Test1()&lt;br /&gt;
        {&lt;br /&gt;
            InternalClass internalClass = new InternalClass(); //Compilation error here&lt;br /&gt;
&lt;br /&gt;
            PublicClass publicClass = new PublicClass; //OK&lt;br /&gt;
            publicClass.Method1(); //Compilation error here&lt;br /&gt;
            publicClass.privateMember1 = 0; //Compilation error here&lt;br /&gt;
            publicClass.Member1 = 0; //Compilation error here&lt;br /&gt;
            publicClass.internalMember2 = 0; //Compilation error here&lt;br /&gt;
            publicClass.publicMember3 = 0; //OK&lt;br /&gt;
            publicClass.protectedInternalMember4 = 0; //Compilation error here&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    internal class DerivedClass : PublicClass&lt;br /&gt;
    {&lt;br /&gt;
        private void Test2()&lt;br /&gt;
        {&lt;br /&gt;
            Member1 = 0; //OK&lt;br /&gt;
            internalMember2 = 0; //Compilation error here&lt;br /&gt;
            publicMember3 = 0; //OK&lt;br /&gt;
            protectedInternalMember4 = 0; //OK&lt;br /&gt;
        }&lt;br /&gt;
        private class NestedClass&lt;br /&gt;
        {&lt;br /&gt;
            void Test(DerivedClass foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.Member1 = 0; //Compilation error here&lt;br /&gt;
                foo.internalMember2 = 0; //Compilation error here&lt;br /&gt;
                foo.publicMember3 = 0; //OK&lt;br /&gt;
                foo.protectedInternalMember4 = 0; //OK&lt;br /&gt;
            }&lt;br /&gt;
            void Test(Class2 foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.Member1 = 0; //Compilation error here&lt;br /&gt;
                foo.internalMember2 = 0; //Compilation error here&lt;br /&gt;
                foo.publicMember3 = 0; //OK&lt;br /&gt;
                foo.protectedInternalMember4 = 0; //Compilation error here&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Access controls is one of the important features provided by the Object Oriented Languages as it helps in achieving Encapsulation.Thus one can make his code less vulnerable from being exploited by others utilizing the features and capabilities provided by the access specifiers . &lt;br /&gt;
The philosophies of access controls in object oriented languages have not changed much over time but certainly the access control across different Object Oriented Languages are different if not completely.&lt;br /&gt;
 &lt;br /&gt;
==References==&lt;br /&gt;
# http://ruby-doc.org/core/&lt;br /&gt;
# http://rubylearning.com/satishtalim/ruby_access_control.html&lt;br /&gt;
# http://phrogz.net/programmingruby/tut_classes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/kktasw36(v=VS.80).aspx&lt;br /&gt;
# http://www.kuzbass.ru:8086/docs/isocpp/access.html&lt;br /&gt;
# http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr130.htm&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx&lt;br /&gt;
# http://stackoverflow.com/questions/2084801/c-using-declaration-scope-and-access-control&lt;br /&gt;
# http://en.wikipedia.org/wiki/Class_(computer_programming)#Member_accessibility&lt;br /&gt;
# http://www.jvoegele.com/software/langcomp.html&lt;br /&gt;
# http://csharp.net-informations.com/language/csharp-access-specifiers.htm&lt;br /&gt;
# http://blogs.oberon.ch/tamberg/2007-10-30/understanding-csharp-access-modifiers.html&lt;br /&gt;
# http://www.enscand.com/howtos/access/access.html&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2b_sa&amp;diff=51662</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2b_sa&amp;diff=51662"/>
		<updated>2011-10-01T19:12:36Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Access Control  in Object Oriented Languages'''&lt;br /&gt;
:Each Object Oriented Language has its own way of implementing Access Control .This page discusses the various ways in which Access control is implemented in some of the Object Oriented Languages. &lt;br /&gt;
=='''Access Control'''==&lt;br /&gt;
Access control in programming languages provides the programmer the ability to define standards that are needed to hide the implementation of the modules from the public interface.Access control provides the programmer ,the ability to prevent under privileged users from access to internal components of a program and restricts them from changing the data within the component to an inconsistent state(i.e elements(members functions and variables) within a program(class)) there by protecting the integrity of the components .This concept of hiding the information in object oriented languages is called [http://en.wikipedia.org/wiki/Encapsulation Encapsulation].&lt;br /&gt;
&lt;br /&gt;
==Access Control in Ruby==&lt;br /&gt;
Access control in ruby is achieved using the following [http://en.wikipedia.org/wiki/Java_syntax#Access_modifiers access modifiers]for the methods.&lt;br /&gt;
*''public''    :The methods that are declared as public can be accessed by any object belong to the class .&lt;br /&gt;
*''private''   :The methods that are declared as private can only be invoked in the context of the current object.&lt;br /&gt;
*''protected'' :The methods that are declared as protected can be invoked by the objects of both the class and the sub-classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby these modifiers only apply to the methods but not the instance variables.&lt;br /&gt;
&lt;br /&gt;
====Public Methods in Ruby====&lt;br /&gt;
Public methods in ruby can be accessed by any object or instance of a Class and its Sub-classes.&lt;br /&gt;
Declaration of  public methods:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;      &lt;br /&gt;
class A              &lt;br /&gt;
  def method     &lt;br /&gt;
    puts &amp;quot;hello&amp;quot;    &lt;br /&gt;
  end               &lt;br /&gt;
end          &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;      &lt;br /&gt;
class A              &lt;br /&gt;
  def method     &lt;br /&gt;
    puts &amp;quot;hello&amp;quot;    &lt;br /&gt;
  end &lt;br /&gt;
  public :method              &lt;br /&gt;
end               &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Private Methods in Ruby====&lt;br /&gt;
private methods in Ruby can only be invoked by the current object in question and one cannot invoke other object's private methods.&lt;br /&gt;
In Ruby private methods are not private to the class but they are private to the objects. No method is perfectly private in Ruby.&lt;br /&gt;
&lt;br /&gt;
======Declaration of Private methods======&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A &lt;br /&gt;
 private  &lt;br /&gt;
   def method&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A&lt;br /&gt;
   def method&lt;br /&gt;
   end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example to show that private methods in Ruby can only be invoked by the current object in context.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It gives an error because pubm method is called in the context of object 'a',so only object 'a' can &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        invoke the private method but not the new object. &lt;br /&gt;
  end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B&lt;br /&gt;
  def m1&lt;br /&gt;
     method                         //private methods of the base class can be accessed by the sub class&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
b=B.new&lt;br /&gt;
a.pubm&lt;br /&gt;
a.method                           //gives an error&lt;br /&gt;
b.m1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Protected Methods in Ruby===&lt;br /&gt;
protected methods are visible in both the base class as well as the sub-classes(i.e they can be invoked by the objects of both the class and its sub-classes).&lt;br /&gt;
&lt;br /&gt;
Declaration of protected methods is ruby.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
 protected&lt;br /&gt;
  def method&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
  end&lt;br /&gt;
 protected :method&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Unlike private methods ,In Ruby protected methods can be called with an explicit receiver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a protected method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It does not give an error because in the context of the current object, another object belonging to same &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        class can invoke the protected method .&lt;br /&gt;
  end&lt;br /&gt;
  protected :method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B&lt;br /&gt;
  def m1&lt;br /&gt;
     method                         //protected methods of the base class can be accessed by the sub class.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
b=B.new&lt;br /&gt;
a.pubm&lt;br /&gt;
a.method                           //gives an error&lt;br /&gt;
b.m1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby both the private as well as the protected methods can be accessed from outside the class using send method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def privateMethod&lt;br /&gt;
    puts &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    privateMethod&lt;br /&gt;
    A.new.method                     // It does not give an error because in the context of the current object, another object belonging to same &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        class can invoke the protected method .&lt;br /&gt;
  end&lt;br /&gt;
  private :privateMethod&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
a.method                       // gives an error&lt;br /&gt;
a.send(:privateMethod)                // prints &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
a.send :privateMethod                 // prints &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
a.method(:privateMethod).call  // prints &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Access Control in C++==&lt;br /&gt;
&lt;br /&gt;
===Controlling Access to the Member Data and Member Functions===&lt;br /&gt;
&lt;br /&gt;
In C++ one can restrict the level of access control on the member data a&lt;br /&gt;
nd function using the access specifiers public,private and protected.&lt;br /&gt;
A constructor should never be declared as private.&lt;br /&gt;
Access control is applied to all the names uniformly.&lt;br /&gt;
&lt;br /&gt;
===Controlling the Level of accessibility of Base class and its Members===&lt;br /&gt;
If a class is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) inherited]using a public access specifier than the public members of the base class are inherited as the public members of the derived class and  protected members of the base class are inherited as the protected members of the derived class.If a class is inherited using a private access specifier than both the public and protected members of the base class become the private members of the derived class.If a class is inherited using a protected access specifier than both the public and protected members of the base class become the protected. members of the derived class.&lt;br /&gt;
[[File:Access.png|400px|center|upright=0.56|Diagram Depicting the Visibility of members and the Inheritance effect.]]&lt;br /&gt;
However, the sub-class members can determine the values of the  base class private members by using the Base class public member functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
      int a;&lt;br /&gt;
      public:&lt;br /&gt;
             int value()&lt;br /&gt;
             {&lt;br /&gt;
                 a=10;&lt;br /&gt;
                 return a;&lt;br /&gt;
             }&lt;br /&gt;
};&lt;br /&gt;
class B:public A&lt;br /&gt;
{&lt;br /&gt;
      public:     &lt;br /&gt;
             int b;&lt;br /&gt;
             void bvalue()&lt;br /&gt;
             {&lt;br /&gt;
                 b=value();  //now b is assigned a value 10 .This is how the sub class can determine the value of the base class private member &lt;br /&gt;
                 printf(&amp;quot;%d&amp;quot;,b);  variables        &lt;br /&gt;
             }&lt;br /&gt;
};&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
     B o;&lt;br /&gt;
     o.bvalue();&lt;br /&gt;
     return 1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding] can be used to access the members of the derived class using the Base class pointer.Dynamic binding or late-binding or run time binding is achieved using virtual functions in the Base class.If the Base class pointer pointing to the derived class object and the matching function is declared virtual in base class, then which function is call is decided at run-time using virtual table entry.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Friendly Functions===&lt;br /&gt;
Like ''send'' method in Ruby,C++ allow the Friend Functions which are not within the scope of the class to access the private members of the class. The friend function are defined like any other function without using either the keyword friend or [http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7l.doc%2Flanguage%2Fref%2Fclrc05cplr175.htm :: scope resolution operator].Since they are not within the scope of the class ,they cannot be called using the objects of the class.Declaration of a friend function in either the public or private part of the class does not change it functionality.&lt;br /&gt;
&lt;br /&gt;
===Friend Classes===&lt;br /&gt;
A class declared as a [http://en.wikipedia.org/wiki/Friend_class friend class] of an another class can access all the private and protected members of the class extending friendship.&lt;br /&gt;
&lt;br /&gt;
==Access Control in Java==&lt;br /&gt;
Access control can be defined as the methods that limit accessibility of the resources to a set of users or programs to enforce integrity, confidentiality or availability constraints. &lt;br /&gt;
Java provides two levels of access controls&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Top Level&amp;quot; – Public, or Package-Private (no explicit modifier, also known as default).&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Member Level&amp;quot; – Public, Private, Protected or Package-Private (no explicit modifier, also known as default).&lt;br /&gt;
===Top Level===&lt;br /&gt;
On the Top level come classes and interfaces. An Interface can only be declared as public. While a class can be declared to be either public or default. If a class is declared as public then it is visible to all the classes in the same or a different package. But if a class is declared with no access specifier i.e. default, then it is public in its own package i.e. it cannot be accessed outside the package other than the one in which it is declared.&lt;br /&gt;
===Member Level===&lt;br /&gt;
At the member level we can declare fields, methods and constructors as public, private, protected and default. Public and default modifiers are used in the same way as the top level and with the same meaning. When a member of a class is declared as private then it can be accessed only within the class. Also, when a member is declared as protected then it can be accessed by all the classes in the same package and the sub classes (of its class) in other packages.&lt;br /&gt;
===Final Keyword===&lt;br /&gt;
&lt;br /&gt;
When this keyword is used along with the class declaration, the class cannot be inherited by any other class.If a method is declared to be final then that method cannot be overridden and if a variable is declared as final then its value cannot be changed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
To understand it in more detail consider the following the example.&lt;br /&gt;
[[File:Access1.png|400px|center]]&lt;br /&gt;
In the example each package contains two classes. Class C is a sub class of class A. Assuming class A as public then its members can be accessed like –&lt;br /&gt;
Class A – It can access all its members independent of their access modifier.&lt;br /&gt;
Class B – It can access all the members of class A except those are private. &lt;br /&gt;
Class C – It can access public and protected members of class A because it is a sub class of class A.&lt;br /&gt;
Class D – It can access only the public members of class A. &lt;br /&gt;
The access modifiers of the top level component govern whether its members are available in the other packages i.e. even if we are having public methods in a class but the class is declared as default then its methods will not be available in other packages. &lt;br /&gt;
If one’s class is going to be used by other coders then he must ensure that errors due to misuse do not happen. For this he must provide the most restrictive access to its members like always declare the fields as private and provide methods to access/modify them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package package1;&lt;br /&gt;
&lt;br /&gt;
public class Parent {&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * &lt;br /&gt;
	 */&lt;br /&gt;
	private int privateVariable;&lt;br /&gt;
	public int publicVariable;&lt;br /&gt;
	protected int protectedVariable;&lt;br /&gt;
	int defaultVariable;&lt;br /&gt;
	&lt;br /&gt;
	private int getPrivateVariable() {&lt;br /&gt;
		return privateVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	private void setPrivateVariable(int privateVariable) {&lt;br /&gt;
		this.privateVariable = privateVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public int getPublicVariable() {&lt;br /&gt;
		return publicVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public void setPublicVariable(int publicVariable) {&lt;br /&gt;
		this.publicVariable = publicVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	protected int getProtectedVariable() {&lt;br /&gt;
		return protectedVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	protected void setProtectedVariable(int protectedVariable) {&lt;br /&gt;
		this.protectedVariable = protectedVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	int getDefaultVariable() {&lt;br /&gt;
		return defaultVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	void setDefaultVariable(int defaultVariable) {&lt;br /&gt;
		this.defaultVariable = defaultVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		// TODO Auto-generated method stub&lt;br /&gt;
		Parent parent=new Parent();&lt;br /&gt;
		parent.setDefaultVariable(5);&lt;br /&gt;
		parent.setPrivateVariable(4);&lt;br /&gt;
		parent.setProtectedVariable(6);&lt;br /&gt;
		parent.setPublicVariable(9);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
package package1;&lt;br /&gt;
&lt;br /&gt;
public class ChildInSamePackage extends Parent{&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Public, Protected and Default members available.&lt;br /&gt;
	 */&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		// TODO Auto-generated method stub&lt;br /&gt;
		ChildInSamePackage childInSamePackage = new ChildInSamePackage();&lt;br /&gt;
		childInSamePackage.setDefaultVariable(1);&lt;br /&gt;
		childInSamePackage.setProtectedVariable(5);&lt;br /&gt;
		childInSamePackage.setPublicVariable(7);&lt;br /&gt;
		//childInSamePackage.setPrivateVariable(7); // not allowed. Compile time Error&lt;br /&gt;
		&lt;br /&gt;
		Parent parent =new Parent();&lt;br /&gt;
		parent.setDefaultVariable(0);&lt;br /&gt;
		parent.setProtectedVariable(1);&lt;br /&gt;
		parent.setPublicVariable(0);&lt;br /&gt;
		//parent.setPrivateVariable(0); //not allowed. compile time error&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
package package1;&lt;br /&gt;
&lt;br /&gt;
public class ClassInSamePackage {&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Public, Protected and Default available but only to parent class object.&lt;br /&gt;
	 */&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		// TODO Auto-generated method stub&lt;br /&gt;
		Parent parent = new Parent();&lt;br /&gt;
		parent.setDefaultVariable(0);&lt;br /&gt;
		parent.setProtectedVariable(5);&lt;br /&gt;
		parent.setPublicVariable(9);&lt;br /&gt;
		//parent.setPrivateVariable(9); //not allowed&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
package package2;&lt;br /&gt;
&lt;br /&gt;
import package1.Parent;&lt;br /&gt;
&lt;br /&gt;
public class ChildInDifferentPackage extends Parent{&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Both public and Protected members can be accessed. &lt;br /&gt;
	 * But Protected can be accessed only through inheritance.&lt;br /&gt;
	 */&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		// TODO Auto-generated method stub&lt;br /&gt;
		ChildInDifferentPackage childInDifferentPackage = new ChildInDifferentPackage();&lt;br /&gt;
		childInDifferentPackage.setProtectedVariable(5);&lt;br /&gt;
		childInDifferentPackage.setPublicVariable(4);&lt;br /&gt;
		&lt;br /&gt;
		Parent parent = new Parent();&lt;br /&gt;
		parent.setPublicVariable(7);&lt;br /&gt;
		//parent.setProtectedVariable(7); // not allowed. protected members are available only through inheritance.&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
package package2;&lt;br /&gt;
&lt;br /&gt;
import package1.Parent;&lt;br /&gt;
&lt;br /&gt;
public class ClassInDifferentPackage {&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Only public members of parent class can be accessed &lt;br /&gt;
	 * but using the object of parent class.  &lt;br /&gt;
	 */&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
				&lt;br /&gt;
		Parent parent = new Parent();&lt;br /&gt;
		parent.setPublicVariable(4); // only public members are available.&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Access Control in C#==&lt;br /&gt;
The accessibility levels of the types (class, interface, struct, enum etc.) and the members, controls whether they can be used from the other code in our assembly (*) or other assemblies.&lt;br /&gt;
&lt;br /&gt;
===Accessibility levels provided by C#===&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Public&amp;quot;: It can be used for the types and type members. It allows them to be accessed everywhere in the same assembly or the other assembly.&lt;br /&gt;
*&amp;quot;Private&amp;quot;: It can be used only for the members. Private members are accessible only within the class or the struct in which they are declared. &lt;br /&gt;
*&amp;quot;Protected&amp;quot;: It is a member access modifier. It allows the class to hide its members from the other classes except the child classes. &lt;br /&gt;
*&amp;quot;Internal&amp;quot;: It can be used both for the types and type members. Internal types and members are accessible only by the code in the same assembly.&lt;br /&gt;
*&amp;quot;Protected Internal&amp;quot;: It’s a combination of protected and internal modifier. It allows the types and the members to be used by any code in the same assembly or by any derived class in another assembly. A protected internal element can be accessed in a derived class in another assembly by using an instance of the derived class.&lt;br /&gt;
[[File:Access2.png|400px|center|upright=0.56]]&lt;br /&gt;
&lt;br /&gt;
Usage of these access modifiers for all types or members is dependent on the conditions like the accessibility of the container restricts the accessibility of a type member. &lt;br /&gt;
*- Assembly is a physical unit that can be executed, deployed, versioned and secured.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                                &lt;br /&gt;
    internal class InternalClass&lt;br /&gt;
    {&lt;br /&gt;
        private int privateMember1;&lt;br /&gt;
        protected int protectedMember2;&lt;br /&gt;
        internal int internalMember3;&lt;br /&gt;
        protected internal int protectedInternalMember4;&lt;br /&gt;
&lt;br /&gt;
        private class NestedClass&lt;br /&gt;
        {&lt;br /&gt;
            void Test(Class1 foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.Member1 = 0; //OK&lt;br /&gt;
                foo.Member2 = 0; //OK&lt;br /&gt;
                foo.Member3 = 0; //OK&lt;br /&gt;
                foo.Member4 = 0; //OK&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public class PublicClass&lt;br /&gt;
    {&lt;br /&gt;
        private void Method1()&lt;br /&gt;
        {&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        private int privateMember1;&lt;br /&gt;
&lt;br /&gt;
        protected int Member1&lt;br /&gt;
        {&lt;br /&gt;
            get&lt;br /&gt;
            {&lt;br /&gt;
                return privateMember1;&lt;br /&gt;
            }&lt;br /&gt;
            set&lt;br /&gt;
            {&lt;br /&gt;
                Method1();&lt;br /&gt;
                privateMember1 = value;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        internal int internalMember2;&lt;br /&gt;
        public int publicMember3;&lt;br /&gt;
        protected internal int protectedInternalMember4;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class Test&lt;br /&gt;
    {&lt;br /&gt;
        void Test1()&lt;br /&gt;
        {&lt;br /&gt;
            InternalClass internalClass = new InternalClass(); //OK&lt;br /&gt;
            internalClass.privateMember1 = 0; //Compilation error here&lt;br /&gt;
            internalClass.protectedMember2 = 0; //Compilation error here&lt;br /&gt;
            internalClass.internalMember3 = 0; //OK&lt;br /&gt;
            internalClass.protectedInternalMember4 = 0; //OK&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    internal class DerivedClass : InternalClass&lt;br /&gt;
    {&lt;br /&gt;
        private void Test2()&lt;br /&gt;
        {&lt;br /&gt;
            privateMember1 = 0; //Compilation error here&lt;br /&gt;
            protectedMember2 = 0; //OK&lt;br /&gt;
            internalMember3 = 0; //OK&lt;br /&gt;
            protectedInternalMember4 = 0; //OK&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        private class NestedClass&lt;br /&gt;
        {&lt;br /&gt;
            void Test(DerivedClass foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.privateMember1 = 0; //Compilation error here&lt;br /&gt;
                foo.protectedMember2 = 0; //OK&lt;br /&gt;
                foo.internalMember3 = 0; //OK&lt;br /&gt;
                foo.protectedInternalMember4 = 0; //OK&lt;br /&gt;
            }&lt;br /&gt;
            void Test(Class1 foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.privateMember1 = 0; //Compilation error here&lt;br /&gt;
                foo.protectedMember2 = 0; //Compilation error here&lt;br /&gt;
                foo.internalMember3 = 0; //OK&lt;br /&gt;
                foo.protectedInternalMember4 = 0; //OK&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
------------------    Class in a different assembly     -------------------------------&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Test&lt;br /&gt;
    {&lt;br /&gt;
        void Test1()&lt;br /&gt;
        {&lt;br /&gt;
            InternalClass internalClass = new InternalClass(); //Compilation error here&lt;br /&gt;
&lt;br /&gt;
            PublicClass publicClass = new PublicClass; //OK&lt;br /&gt;
            publicClass.Method1(); //Compilation error here&lt;br /&gt;
            publicClass.privateMember1 = 0; //Compilation error here&lt;br /&gt;
            publicClass.Member1 = 0; //Compilation error here&lt;br /&gt;
            publicClass.internalMember2 = 0; //Compilation error here&lt;br /&gt;
            publicClass.publicMember3 = 0; //OK&lt;br /&gt;
            publicClass.protectedInternalMember4 = 0; //Compilation error here&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    internal class DerivedClass : PublicClass&lt;br /&gt;
    {&lt;br /&gt;
        private void Test2()&lt;br /&gt;
        {&lt;br /&gt;
            Member1 = 0; //OK&lt;br /&gt;
            internalMember2 = 0; //Compilation error here&lt;br /&gt;
            publicMember3 = 0; //OK&lt;br /&gt;
            protectedInternalMember4 = 0; //OK&lt;br /&gt;
        }&lt;br /&gt;
        private class NestedClass&lt;br /&gt;
        {&lt;br /&gt;
            void Test(DerivedClass foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.Member1 = 0; //Compilation error here&lt;br /&gt;
                foo.internalMember2 = 0; //Compilation error here&lt;br /&gt;
                foo.publicMember3 = 0; //OK&lt;br /&gt;
                foo.protectedInternalMember4 = 0; //OK&lt;br /&gt;
            }&lt;br /&gt;
            void Test(Class2 foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.Member1 = 0; //Compilation error here&lt;br /&gt;
                foo.internalMember2 = 0; //Compilation error here&lt;br /&gt;
                foo.publicMember3 = 0; //OK&lt;br /&gt;
                foo.protectedInternalMember4 = 0; //Compilation error here&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Access controls is one of the important features provided by the Object Oriented Languages as it helps in achieving Encapsulation.Thus one can make his code less vulnerable from being exploited by others utilizing the features and capabilities provided by the access specifiers . &lt;br /&gt;
The philosophies of access controls in object oriented languages have not changed much over time but certainly the access control across different Object Oriented Languages are different if not completely.&lt;br /&gt;
 &lt;br /&gt;
==References==&lt;br /&gt;
# http://ruby-doc.org/core/&lt;br /&gt;
# http://rubylearning.com/satishtalim/ruby_access_control.html&lt;br /&gt;
# http://phrogz.net/programmingruby/tut_classes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/kktasw36(v=VS.80).aspx&lt;br /&gt;
# http://www.kuzbass.ru:8086/docs/isocpp/access.html&lt;br /&gt;
# http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr130.htm&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx&lt;br /&gt;
# http://stackoverflow.com/questions/2084801/c-using-declaration-scope-and-access-control&lt;br /&gt;
# http://en.wikipedia.org/wiki/Class_(computer_programming)#Member_accessibility&lt;br /&gt;
# http://www.jvoegele.com/software/langcomp.html&lt;br /&gt;
# http://csharp.net-informations.com/language/csharp-access-specifiers.htm&lt;br /&gt;
# http://blogs.oberon.ch/tamberg/2007-10-30/understanding-csharp-access-modifiers.html&lt;br /&gt;
# http://www.enscand.com/howtos/access/access.html&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2b_sa&amp;diff=51661</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2b_sa&amp;diff=51661"/>
		<updated>2011-10-01T18:54:35Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Access Control  in Object Oriented Languages'''&lt;br /&gt;
:Each Object Oriented Language has its own way of implementing Access Control .This page discusses the various ways in which Access control is implemented in some of the Object Oriented Languages. &lt;br /&gt;
=='''Access Control'''==&lt;br /&gt;
Access control in programming languages provides the programmer the ability to define standards that are needed to hide the implementation of the modules from the public interface.Access control provides the programmer ,the ability to prevent under privileged users from access to internal components of a program and restricts them from changing the data within the component to an inconsistent state(i.e elements(members functions and variables) within a program(class)) there by protecting the integrity of the components .This concept of hiding the information in object oriented languages is called [http://en.wikipedia.org/wiki/Encapsulation Encapsulation].&lt;br /&gt;
&lt;br /&gt;
==Access Control in Ruby==&lt;br /&gt;
Access control in ruby is achieved using the following [http://en.wikipedia.org/wiki/Java_syntax#Access_modifiers access modifiers]for the methods.&lt;br /&gt;
*''public''    :The methods that are declared as public can be accessed by any object belong to the class .&lt;br /&gt;
*''private''   :The methods that are declared as private can only be invoked in the context of the current object.&lt;br /&gt;
*''protected'' :The methods that are declared as protected can be invoked by the objects of both the class and the sub-classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby these modifiers only apply to the methods but not the instance variables.&lt;br /&gt;
&lt;br /&gt;
====Public Methods in Ruby====&lt;br /&gt;
Public methods in ruby can be accessed by any object or instance of a Class and its Sub-classes.&lt;br /&gt;
Declaration of  public methods:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;      &lt;br /&gt;
class A              &lt;br /&gt;
  def method     &lt;br /&gt;
    puts &amp;quot;hello&amp;quot;    &lt;br /&gt;
  end               &lt;br /&gt;
end          &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;      &lt;br /&gt;
class A              &lt;br /&gt;
  def method     &lt;br /&gt;
    puts &amp;quot;hello&amp;quot;    &lt;br /&gt;
  end &lt;br /&gt;
  public :method              &lt;br /&gt;
end               &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Private Methods in Ruby====&lt;br /&gt;
private methods in Ruby can only be invoked by the current object in question and one cannot invoke other object's private methods.&lt;br /&gt;
In Ruby private methods are not private to the class but they are private to the objects. No method is perfectly private in Ruby.&lt;br /&gt;
&lt;br /&gt;
======Declaration of Private methods======&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A &lt;br /&gt;
 private  &lt;br /&gt;
   def method&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A&lt;br /&gt;
   def method&lt;br /&gt;
   end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example to show that private methods in Ruby can only be invoked by the current object in context.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It gives an error because pubm method is called in the context of object 'a',so only object 'a' can &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        invoke the private method but not the new object. &lt;br /&gt;
  end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B&lt;br /&gt;
  def m1&lt;br /&gt;
     method                         //private methods of the base class can be accessed by the sub class&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
b=B.new&lt;br /&gt;
a.pubm&lt;br /&gt;
a.method                           //gives an error&lt;br /&gt;
b.m1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Protected Methods in Ruby===&lt;br /&gt;
protected methods are visible in both the base class as well as the sub-classes(i.e they can be invoked by the objects of both the class and its sub-classes).&lt;br /&gt;
&lt;br /&gt;
Declaration of protected methods is ruby.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
 protected&lt;br /&gt;
  def method&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
  end&lt;br /&gt;
 protected :method&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Unlike private methods ,In Ruby protected methods can be called with an explicit receiver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a protected method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It does not give an error because in the context of the current object, another object belonging to same &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        class can invoke the protected method .&lt;br /&gt;
  end&lt;br /&gt;
  protected :method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B&lt;br /&gt;
  def m1&lt;br /&gt;
     method                         //protected methods of the base class can be accessed by the sub class.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
b=B.new&lt;br /&gt;
a.pubm&lt;br /&gt;
a.method                           //gives an error&lt;br /&gt;
b.m1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby both the private as well as the protected methods can be accessed from outside the class using send method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def privateMethod&lt;br /&gt;
    puts &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    privateMethod&lt;br /&gt;
    A.new.method                     // It does not give an error because in the context of the current object, another object belonging to same &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        class can invoke the protected method .&lt;br /&gt;
  end&lt;br /&gt;
  private :privateMethod&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
a.method                       // gives an error&lt;br /&gt;
a.send(:privateMethod)                // prints &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
a.send :privateMethod                 // prints &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
a.method(:privateMethod).call  // prints &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Access Control in C++==&lt;br /&gt;
&lt;br /&gt;
===Controlling Access to the Member Data and Member Functions===&lt;br /&gt;
&lt;br /&gt;
In C++ one can restrict the level of access control on the member data a&lt;br /&gt;
nd function using the access specifiers public,private and protected.&lt;br /&gt;
A constructor should never be declared as private.&lt;br /&gt;
Access control is applied to all the names uniformly.&lt;br /&gt;
&lt;br /&gt;
===Controlling the Level of accessibility of Base class and its Members===&lt;br /&gt;
If a class is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) inherited]using a public access specifier than the public members of the base class are inherited as the public members of the derived class and  protected members of the base class are inherited as the protected members of the derived class.If a class is inherited using a private access specifier than both the public and protected members of the base class become the private members of the derived class.If a class is inherited using a protected access specifier than both the public and protected members of the base class become the protected. members of the derived class.&lt;br /&gt;
[[File:Access.png|400px|center|upright=0.56|Diagram Depicting the Visibility of members and the Inheritance effect.]]&lt;br /&gt;
However, the sub-class members can determine the values of the  base class private members by using the Base class public member functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
      int a;&lt;br /&gt;
      public:&lt;br /&gt;
             int value()&lt;br /&gt;
             {&lt;br /&gt;
                 a=10;&lt;br /&gt;
                 return a;&lt;br /&gt;
             }&lt;br /&gt;
};&lt;br /&gt;
class B:public A&lt;br /&gt;
{&lt;br /&gt;
      public:     &lt;br /&gt;
             int b;&lt;br /&gt;
             void bvalue()&lt;br /&gt;
             {&lt;br /&gt;
                 b=value();  //now b is assigned a value 10 .This is how the sub class can determine the value of the base class private member &lt;br /&gt;
                 printf(&amp;quot;%d&amp;quot;,b);  variables        &lt;br /&gt;
             }&lt;br /&gt;
};&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
     B o;&lt;br /&gt;
     o.bvalue();&lt;br /&gt;
     return 1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding] can be used to access the members of the derived class using the Base class pointer.Dynamic binding or late-binding or run time binding is achieved using virtual functions in the Base class.If the Base class pointer pointing to the derived class object and the matching function is declared virtual in base class, then which function is call is decided at run-time using virtual table entry.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Friendly Functions===&lt;br /&gt;
Like ''send'' method in Ruby,C++ allow the Friend Functions which are not within the scope of the class to access the private members of the class. The friend function are defined like any other function without using either the keyword friend or [http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7l.doc%2Flanguage%2Fref%2Fclrc05cplr175.htm :: scope resolution operator].Since they are not within the scope of the class ,they cannot be called using the objects of the class.Declaration of a friend function in either the public or private part of the class does not change it functionality.&lt;br /&gt;
&lt;br /&gt;
===Friend Classes===&lt;br /&gt;
A class declared as a [http://en.wikipedia.org/wiki/Friend_class friend class] of an another class can access all the private and protected members of the class extending friendship.&lt;br /&gt;
&lt;br /&gt;
==Access Control in Java==&lt;br /&gt;
Access control can be defined as the methods that limit accessibility of the resources to a set of users or programs to enforce integrity, confidentiality or availability constraints. &lt;br /&gt;
Java provides two levels of access controls&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Top Level&amp;quot; – Public, or Package-Private (no explicit modifier, also known as default).&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Member Level&amp;quot; – Public, Private, Protected or Package-Private (no explicit modifier, also known as default).&lt;br /&gt;
===Top Level===&lt;br /&gt;
On the Top level come classes and interfaces. An Interface can only be declared as public. While a class can be declared to be either public or default. If a class is declared as public then it is visible to all the classes in the same or a different package. But if a class is declared with no access specifier i.e. default, then it is public in its own package i.e. it cannot be accessed outside the package other than the one in which it is declared.&lt;br /&gt;
===Member Level===&lt;br /&gt;
At the member level we can declare fields, methods and constructors as public, private, protected and default. Public and default modifiers are used in the same way as the top level and with the same meaning. When a member of a class is declared as private then it can be accessed only within the class. Also, when a member is declared as protected then it can be accessed by all the classes in the same package and the sub classes (of its class) in other packages.&lt;br /&gt;
===Final Keyword===&lt;br /&gt;
&lt;br /&gt;
When this keyword is used along with the class declaration, the class cannot be inherited by any other class.If a method is declared to be final then that method cannot be overridden and if a variable is declared as final then its value cannot be changed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
To understand it in more detail consider the following the example.&lt;br /&gt;
[[File:Access1.png|400px|center]]&lt;br /&gt;
In the example each package contains two classes. Class C is a sub class of class A. Assuming class A as public then its members can be accessed like –&lt;br /&gt;
Class A – It can access all its members independent of their access modifier.&lt;br /&gt;
Class B – It can access all the members of class A except those are private. &lt;br /&gt;
Class C – It can access public and protected members of class A because it is a sub class of class A.&lt;br /&gt;
Class D – It can access only the public members of class A. &lt;br /&gt;
The access modifiers of the top level component govern whether its members are available in the other packages i.e. even if we are having public methods in a class but the class is declared as default then its methods will not be available in other packages. &lt;br /&gt;
If one’s class is going to be used by other coders then he must ensure that errors due to misuse do not happen. For this he must provide the most restrictive access to its members like always declare the fields as private and provide methods to access/modify them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package package1;&lt;br /&gt;
&lt;br /&gt;
public class Parent {&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * &lt;br /&gt;
	 */&lt;br /&gt;
	private int privateVariable;&lt;br /&gt;
	public int publicVariable;&lt;br /&gt;
	protected int protectedVariable;&lt;br /&gt;
	int defaultVariable;&lt;br /&gt;
	&lt;br /&gt;
	private int getPrivateVariable() {&lt;br /&gt;
		return privateVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	private void setPrivateVariable(int privateVariable) {&lt;br /&gt;
		this.privateVariable = privateVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public int getPublicVariable() {&lt;br /&gt;
		return publicVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public void setPublicVariable(int publicVariable) {&lt;br /&gt;
		this.publicVariable = publicVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	protected int getProtectedVariable() {&lt;br /&gt;
		return protectedVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	protected void setProtectedVariable(int protectedVariable) {&lt;br /&gt;
		this.protectedVariable = protectedVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	int getDefaultVariable() {&lt;br /&gt;
		return defaultVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	void setDefaultVariable(int defaultVariable) {&lt;br /&gt;
		this.defaultVariable = defaultVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		// TODO Auto-generated method stub&lt;br /&gt;
		Parent parent=new Parent();&lt;br /&gt;
		parent.setDefaultVariable(5);&lt;br /&gt;
		parent.setPrivateVariable(4);&lt;br /&gt;
		parent.setProtectedVariable(6);&lt;br /&gt;
		parent.setPublicVariable(9);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
package package1;&lt;br /&gt;
&lt;br /&gt;
public class ChildInSamePackage extends Parent{&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Public, Protected and Default members available.&lt;br /&gt;
	 */&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		// TODO Auto-generated method stub&lt;br /&gt;
		ChildInSamePackage childInSamePackage = new ChildInSamePackage();&lt;br /&gt;
		childInSamePackage.setDefaultVariable(1);&lt;br /&gt;
		childInSamePackage.setProtectedVariable(5);&lt;br /&gt;
		childInSamePackage.setPublicVariable(7);&lt;br /&gt;
		//childInSamePackage.setPrivateVariable(7); // not allowed. Compile time Error&lt;br /&gt;
		&lt;br /&gt;
		Parent parent =new Parent();&lt;br /&gt;
		parent.setDefaultVariable(0);&lt;br /&gt;
		parent.setProtectedVariable(1);&lt;br /&gt;
		parent.setPublicVariable(0);&lt;br /&gt;
		//parent.setPrivateVariable(0); //not allowed. compile time error&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
package package1;&lt;br /&gt;
&lt;br /&gt;
public class ClassInSamePackage {&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Public, Protected and Default available but only to parent class object.&lt;br /&gt;
	 */&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		// TODO Auto-generated method stub&lt;br /&gt;
		Parent parent = new Parent();&lt;br /&gt;
		parent.setDefaultVariable(0);&lt;br /&gt;
		parent.setProtectedVariable(5);&lt;br /&gt;
		parent.setPublicVariable(9);&lt;br /&gt;
		//parent.setPrivateVariable(9); //not allowed&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
package package2;&lt;br /&gt;
&lt;br /&gt;
import package1.Parent;&lt;br /&gt;
&lt;br /&gt;
public class ChildInDifferentPackage extends Parent{&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Both public and Protected members can be accessed. &lt;br /&gt;
	 * But Protected can be accessed only through inheritance.&lt;br /&gt;
	 */&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		// TODO Auto-generated method stub&lt;br /&gt;
		ChildInDifferentPackage childInDifferentPackage = new ChildInDifferentPackage();&lt;br /&gt;
		childInDifferentPackage.setProtectedVariable(5);&lt;br /&gt;
		childInDifferentPackage.setPublicVariable(4);&lt;br /&gt;
		&lt;br /&gt;
		Parent parent = new Parent();&lt;br /&gt;
		parent.setPublicVariable(7);&lt;br /&gt;
		//parent.setProtectedVariable(7); // not allowed. protected members are available only through inheritance.&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
package package2;&lt;br /&gt;
&lt;br /&gt;
import package1.Parent;&lt;br /&gt;
&lt;br /&gt;
public class ClassInDifferentPackage {&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Only public members of parent class can be accessed &lt;br /&gt;
	 * but using the object of parent class.  &lt;br /&gt;
	 */&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
				&lt;br /&gt;
		Parent parent = new Parent();&lt;br /&gt;
		parent.setPublicVariable(4); // only public members are available.&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Access Control in C#==&lt;br /&gt;
The accessibility levels of the types (class, interface, struct, enum etc.) and the members, controls whether they can be used from the other code in our assembly (*) or other assemblies.&lt;br /&gt;
&lt;br /&gt;
===Accessibility levels provided by C#===&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Public&amp;quot;: It can be used for the types and type members. It allows them to be accessed everywhere in the same assembly or the other assembly.&lt;br /&gt;
*&amp;quot;Private&amp;quot;: It can be used only for the members. Private members are accessible only within the class or the struct in which they are declared. &lt;br /&gt;
*&amp;quot;Protected&amp;quot;: It is a member access modifier. It allows the class to hide its members from the other classes except the child classes. &lt;br /&gt;
*&amp;quot;Internal&amp;quot;: It can be used both for the types and type members. Internal types and members are accessible only by the code in the same assembly.&lt;br /&gt;
*&amp;quot;Protected Internal&amp;quot;: It’s a combination of protected and internal modifier. It allows the types and the members to be used by any code in the same assembly or by any derived class in another assembly. A protected internal element can be accessed in a derived class in another assembly by using an instance of the derived class.&lt;br /&gt;
[[File:Access2.png|400px|center|upright=0.56]]&lt;br /&gt;
&lt;br /&gt;
Usage of these access modifiers for all types or members is dependent on the conditions like the accessibility of the container restricts the accessibility of a type member. &lt;br /&gt;
*- Assembly is a physical unit that can be executed, deployed, versioned and secured.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                                &lt;br /&gt;
    internal class InternalClass&lt;br /&gt;
    {&lt;br /&gt;
        private int privateMember1;&lt;br /&gt;
        protected int protectedMember2;&lt;br /&gt;
        internal int internalMember3;&lt;br /&gt;
        protected internal int protectedInternalMember4;&lt;br /&gt;
&lt;br /&gt;
        private class NestedClass&lt;br /&gt;
        {&lt;br /&gt;
            void Test(Class1 foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.Member1 = 0; //OK&lt;br /&gt;
                foo.Member2 = 0; //OK&lt;br /&gt;
                foo.Member3 = 0; //OK&lt;br /&gt;
                foo.Member4 = 0; //OK&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public class PublicClass&lt;br /&gt;
    {&lt;br /&gt;
        private void Method1()&lt;br /&gt;
        {&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        private int privateMember1;&lt;br /&gt;
&lt;br /&gt;
        protected int Member1&lt;br /&gt;
        {&lt;br /&gt;
            get&lt;br /&gt;
            {&lt;br /&gt;
                return privateMember1;&lt;br /&gt;
            }&lt;br /&gt;
            set&lt;br /&gt;
            {&lt;br /&gt;
                Method1();&lt;br /&gt;
                privateMember1 = value;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        internal int internalMember2;&lt;br /&gt;
        public int publicMember3;&lt;br /&gt;
        protected internal int protectedInternalMember4;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class Test&lt;br /&gt;
    {&lt;br /&gt;
        void Test1()&lt;br /&gt;
        {&lt;br /&gt;
            InternalClass internalClass = new InternalClass(); //OK&lt;br /&gt;
            internalClass.privateMember1 = 0; //Compilation error here&lt;br /&gt;
            internalClass.protectedMember2 = 0; //Compilation error here&lt;br /&gt;
            internalClass.internalMember3 = 0; //OK&lt;br /&gt;
            internalClass.protectedInternalMember4 = 0; //OK&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    internal class DerivedClass : InternalClass&lt;br /&gt;
    {&lt;br /&gt;
        private void Test2()&lt;br /&gt;
        {&lt;br /&gt;
            privateMember1 = 0; //Compilation error here&lt;br /&gt;
            protectedMember2 = 0; //OK&lt;br /&gt;
            internalMember3 = 0; //OK&lt;br /&gt;
            protectedInternalMember4 = 0; //OK&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        private class NestedClass&lt;br /&gt;
        {&lt;br /&gt;
            void Test(DerivedClass foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.privateMember1 = 0; //Compilation error here&lt;br /&gt;
                foo.protectedMember2 = 0; //OK&lt;br /&gt;
                foo.internalMember3 = 0; //OK&lt;br /&gt;
                foo.protectedInternalMember4 = 0; //OK&lt;br /&gt;
            }&lt;br /&gt;
            void Test(Class1 foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.privateMember1 = 0; //Compilation error here&lt;br /&gt;
                foo.protectedMember2 = 0; //Compilation error here&lt;br /&gt;
                foo.internalMember3 = 0; //OK&lt;br /&gt;
                foo.protectedInternalMember4 = 0; //OK&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
------------------    Class in a different assembly     -------------------------------&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Test&lt;br /&gt;
    {&lt;br /&gt;
        void Test1()&lt;br /&gt;
        {&lt;br /&gt;
            InternalClass internalClass = new InternalClass(); //Compilation error here&lt;br /&gt;
&lt;br /&gt;
            PublicClass publicClass = new PublicClass; //OK&lt;br /&gt;
            publicClass.Method1(); //Compilation error here&lt;br /&gt;
            publicClass.privateMember1 = 0; //Compilation error here&lt;br /&gt;
            publicClass.Member1 = 0; //Compilation error here&lt;br /&gt;
            publicClass.internalMember2 = 0; //Compilation error here&lt;br /&gt;
            publicClass.publicMember3 = 0; //OK&lt;br /&gt;
            publicClass.protectedInternalMember4 = 0; //Compilation error here&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    internal class DerivedClass : PublicClass&lt;br /&gt;
    {&lt;br /&gt;
        private void Test2()&lt;br /&gt;
        {&lt;br /&gt;
            Member1 = 0; //OK&lt;br /&gt;
            internalMember2 = 0; //Compilation error here&lt;br /&gt;
            publicMember3 = 0; //OK&lt;br /&gt;
            protectedInternalMember4 = 0; //OK&lt;br /&gt;
        }&lt;br /&gt;
        private class NestedClass&lt;br /&gt;
        {&lt;br /&gt;
            void Test(DerivedClass foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.Member1 = 0; //Compilation error here&lt;br /&gt;
                foo.internalMember2 = 0; //Compilation error here&lt;br /&gt;
                foo.publicMember3 = 0; //OK&lt;br /&gt;
                foo.protectedInternalMember4 = 0; //OK&lt;br /&gt;
            }&lt;br /&gt;
            void Test(Class2 foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.Member1 = 0; //Compilation error here&lt;br /&gt;
                foo.internalMember2 = 0; //Compilation error here&lt;br /&gt;
                foo.publicMember3 = 0; //OK&lt;br /&gt;
                foo.protectedInternalMember4 = 0; //Compilation error here&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Conclusion==&lt;br /&gt;
The philosophies of access controls in object oriented languages have not changed much over time but certainly the access control across different Object Oriented Languages are different if not completely.   &lt;br /&gt;
&lt;br /&gt;
Thus we can see that by using the access modifier's each OO language controls the access to its class members.  &lt;br /&gt;
 &lt;br /&gt;
==References==&lt;br /&gt;
# http://ruby-doc.org/core/&lt;br /&gt;
# http://rubylearning.com/satishtalim/ruby_access_control.html&lt;br /&gt;
# http://phrogz.net/programmingruby/tut_classes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/kktasw36(v=VS.80).aspx&lt;br /&gt;
# http://www.kuzbass.ru:8086/docs/isocpp/access.html&lt;br /&gt;
# http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr130.htm&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx&lt;br /&gt;
# http://stackoverflow.com/questions/2084801/c-using-declaration-scope-and-access-control&lt;br /&gt;
# http://en.wikipedia.org/wiki/Class_(computer_programming)#Member_accessibility&lt;br /&gt;
# http://www.jvoegele.com/software/langcomp.html&lt;br /&gt;
# http://csharp.net-informations.com/language/csharp-access-specifiers.htm&lt;br /&gt;
# http://blogs.oberon.ch/tamberg/2007-10-30/understanding-csharp-access-modifiers.html&lt;br /&gt;
# http://www.enscand.com/howtos/access/access.html&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2b_sa&amp;diff=51660</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2b_sa&amp;diff=51660"/>
		<updated>2011-10-01T17:55:50Z</updated>

		<summary type="html">&lt;p&gt;Amaroo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Access Control  in Object Oriented Languages'''&lt;br /&gt;
:Each Object Oriented Language has its own way of implementing Access Control .This page discusses the various ways in which Access control is implemented in some of the Object Oriented Languages. &lt;br /&gt;
=='''Access Control'''==&lt;br /&gt;
Access control in programming languages provides the programmer the ability to define standards that are needed to hide the implementation of the modules from the public interface.Access control provides the programmer ,the ability to prevent under privileged users from access to internal components of a program and restricts them from changing the data within the component to an inconsistent state(i.e elements(members functions and variables) within a program(class)) there by protecting the integrity of the components .This concept of hiding the information in object oriented languages is called [http://en.wikipedia.org/wiki/Encapsulation Encapsulation].&lt;br /&gt;
&lt;br /&gt;
==Access Control in Ruby==&lt;br /&gt;
Access control in ruby is achieved using the following [http://en.wikipedia.org/wiki/Java_syntax#Access_modifiers access modifiers]for the methods.&lt;br /&gt;
*''public''    :The methods that are declared as public can be accessed by any object belong to the class .&lt;br /&gt;
*''private''   :The methods that are declared as private can only be invoked in the context of the current object.&lt;br /&gt;
*''protected'' :The methods that are declared as protected can be invoked by the objects of both the class and the sub-classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby these modifiers only apply to the methods but not the instance variables.&lt;br /&gt;
&lt;br /&gt;
====Public Methods in Ruby====&lt;br /&gt;
Public methods in ruby can be accessed by any object or instance of a Class and its Sub-classes.&lt;br /&gt;
Declaration of  public methods:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;      &lt;br /&gt;
class A              &lt;br /&gt;
  def method     &lt;br /&gt;
    puts &amp;quot;hello&amp;quot;    &lt;br /&gt;
  end               &lt;br /&gt;
end          &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;      &lt;br /&gt;
class A              &lt;br /&gt;
  def method     &lt;br /&gt;
    puts &amp;quot;hello&amp;quot;    &lt;br /&gt;
  end &lt;br /&gt;
  public :method              &lt;br /&gt;
end               &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Private Methods in Ruby====&lt;br /&gt;
private methods in Ruby can only be invoked by the current object in question and one cannot invoke other object's private methods.&lt;br /&gt;
In Ruby private methods are not private to the class but they are private to the objects. No method is perfectly private in Ruby.&lt;br /&gt;
&lt;br /&gt;
======Declaration of Private methods======&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A &lt;br /&gt;
 private  &lt;br /&gt;
   def method&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A&lt;br /&gt;
   def method&lt;br /&gt;
   end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example to show that private methods in Ruby can only be invoked by the current object in context.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It gives an error because pubm method is called in the context of object 'a',so only object 'a' can &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        invoke the private method but not the new object. &lt;br /&gt;
  end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B&lt;br /&gt;
  def m1&lt;br /&gt;
     method                         //private methods of the base class can be accessed by the sub class&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
b=B.new&lt;br /&gt;
a.pubm&lt;br /&gt;
a.method                           //gives an error&lt;br /&gt;
b.m1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Protected Methods in Ruby===&lt;br /&gt;
protected methods are visible in both the base class as well as the sub-classes(i.e they can be invoked by the objects of both the class and its sub-classes).&lt;br /&gt;
&lt;br /&gt;
Declaration of protected methods is ruby.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
 protected&lt;br /&gt;
  def method&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
  end&lt;br /&gt;
 protected :method&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Unlike private methods ,In Ruby protected methods can be called with an explicit receiver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a protected method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It does not give an error because in the context of the current object, another object belonging to same &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        class can invoke the protected method .&lt;br /&gt;
  end&lt;br /&gt;
  protected :method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B&lt;br /&gt;
  def m1&lt;br /&gt;
     method                         //protected methods of the base class can be accessed by the sub class.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
b=B.new&lt;br /&gt;
a.pubm&lt;br /&gt;
a.method                           //gives an error&lt;br /&gt;
b.m1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby both the private as well as the protected methods can be accessed from outside the class using send method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It does not give an error because in the context of the current object, another object belonging to same &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        class can invoke the protected method .&lt;br /&gt;
  end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
a.method                // gives an error&lt;br /&gt;
a.send(:method)         // prints &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
a.send :method          // prints &amp;quot;I am a protected method&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Access Control in C++==&lt;br /&gt;
&lt;br /&gt;
===Controlling Access to the Member Data and Member Functions===&lt;br /&gt;
&lt;br /&gt;
In C++ one can restrict the level of access control on the member data and function using the access specifiers public,private and protected.&lt;br /&gt;
A constructor should never be declared as private.&lt;br /&gt;
Access control is applied to all the names uniformly.&lt;br /&gt;
&lt;br /&gt;
===Controlling the Level of accessibility of Base class and its Members===&lt;br /&gt;
If a class is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) inherited]using a public access specifier than the public members of the base class are inherited as the public members of the derived class and  protected members of the base class are inherited as the protected members of the derived class.If a class is inherited using a private access specifier than both the public and protected members of the base class become the private members of the derived class.If a class is inherited using a protected access specifier than both the public and protected members of the base class become the protected. members of the derived class.&lt;br /&gt;
[[File:Access.png|400px|center|upright=0.56|Diagram Depicting the Visibility of members and the Inheritance effect.]]&lt;br /&gt;
However, the sub-class members can determine the values of the  base class private members by using the Base class public member functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
      int a;&lt;br /&gt;
      public:&lt;br /&gt;
             int value()&lt;br /&gt;
             {&lt;br /&gt;
                 a=10;&lt;br /&gt;
                 return a;&lt;br /&gt;
             }&lt;br /&gt;
};&lt;br /&gt;
class B:public A&lt;br /&gt;
{&lt;br /&gt;
      public:     &lt;br /&gt;
      int b;&lt;br /&gt;
      void bvalue()&lt;br /&gt;
      {&lt;br /&gt;
           b=value();     //now b is assigned a value 10 .This is how the sub class can determine the value of the base class private member &lt;br /&gt;
           printf(&amp;quot;%d&amp;quot;,b);  variables        &lt;br /&gt;
      }&lt;br /&gt;
};&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
    B o;&lt;br /&gt;
    o.bvalue();&lt;br /&gt;
    return 1;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding] can be used to access the members of the derived class using the Base class pointer.Dynamic binding or late-binding or run time binding is achieved using virtual functions in the Base class.If the Base class pointer pointing to the derived class object and the matching function is declared virtual in base class, then which function is call is decided at run-time using virtual table entry.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Friendly Functions===&lt;br /&gt;
Like ''send'' method in Ruby,C++ allow the Friend Functions which are not within the scope of the class to access the private members of the class. The friend function are defined like any other function without using either the keyword friend or [http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7l.doc%2Flanguage%2Fref%2Fclrc05cplr175.htm :: scope resolution operator].Since they are not within the scope of the class ,they cannot be called using the objects of the class.Declaration of a friend function in either the public or private part of the class does not change it functionality.&lt;br /&gt;
&lt;br /&gt;
===Friend Classes===&lt;br /&gt;
A class declared as a [http://en.wikipedia.org/wiki/Friend_class friend class] of an another class can access all the private and protected members of the class extending friendship.&lt;br /&gt;
&lt;br /&gt;
==Access Control in Java==&lt;br /&gt;
Access control can be defined as the methods that limit accessibility of the resources to a set of users or programs to enforce integrity, confidentiality or availability constraints. &lt;br /&gt;
Java provides two levels of access controls&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Top Level&amp;quot; – Public, or Package-Private (no explicit modifier, also known as default).&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Member Level&amp;quot; – Public, Private, Protected or Package-Private (no explicit modifier, also known as default).&lt;br /&gt;
===Top Level===&lt;br /&gt;
On the Top level come classes and interfaces. An Interface can only be declared as public. While a class can be declared to be either public or default. If a class is declared as public then it is visible to all the classes in the same or a different package. But if a class is declared with no access specifier i.e. default, then it is public in its own package i.e. it cannot be accessed outside the package other than the one in which it is declared.&lt;br /&gt;
===Member Level===&lt;br /&gt;
At the member level we can declare fields, methods and constructors as public, private, protected and default. Public and default modifiers are used in the same way as the top level and with the same meaning. When a member of a class is declared as private then it can be accessed only within the class. Also, when a member is declared as protected then it can be accessed by all the classes in the same package and the sub classes (of its class) in other packages. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
To understand it in more detail consider the following the example.&lt;br /&gt;
In the example each package contains two classes. Class C is a sub class of class A. Assuming class A as public then its members can be accessed like –&lt;br /&gt;
Class A – It can access all its members independent of their access modifier.&lt;br /&gt;
Class B – It can access all the members of class A except those are private. &lt;br /&gt;
Class C – It can access public and protected members of class A because it is a sub class of class A.&lt;br /&gt;
Class D – It can access only the public members of class A. &lt;br /&gt;
The access modifiers of the top level component govern whether its members are available in the other packages i.e. even if we are having public methods in a class but the class is declared as default then its methods will not be available in other packages. &lt;br /&gt;
If one’s class is going to be used by other coders then he must ensure that errors due to misuse do not happen. For this he must provide the most restrictive access to its members like always declare the fields as private and provide methods to access/modify them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package package1;&lt;br /&gt;
&lt;br /&gt;
public class Parent {&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * &lt;br /&gt;
	 */&lt;br /&gt;
	private int privateVariable;&lt;br /&gt;
	public int publicVariable;&lt;br /&gt;
	protected int protectedVariable;&lt;br /&gt;
	int defaultVariable;&lt;br /&gt;
	&lt;br /&gt;
	private int getPrivateVariable() {&lt;br /&gt;
		return privateVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	private void setPrivateVariable(int privateVariable) {&lt;br /&gt;
		this.privateVariable = privateVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public int getPublicVariable() {&lt;br /&gt;
		return publicVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public void setPublicVariable(int publicVariable) {&lt;br /&gt;
		this.publicVariable = publicVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	protected int getProtectedVariable() {&lt;br /&gt;
		return protectedVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	protected void setProtectedVariable(int protectedVariable) {&lt;br /&gt;
		this.protectedVariable = protectedVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	int getDefaultVariable() {&lt;br /&gt;
		return defaultVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	void setDefaultVariable(int defaultVariable) {&lt;br /&gt;
		this.defaultVariable = defaultVariable;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		// TODO Auto-generated method stub&lt;br /&gt;
		Parent parent=new Parent();&lt;br /&gt;
		parent.setDefaultVariable(5);&lt;br /&gt;
		parent.setPrivateVariable(4);&lt;br /&gt;
		parent.setProtectedVariable(6);&lt;br /&gt;
		parent.setPublicVariable(9);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
package package1;&lt;br /&gt;
&lt;br /&gt;
public class ChildInSamePackage extends Parent{&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Public, Protected and Default members available.&lt;br /&gt;
	 */&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		// TODO Auto-generated method stub&lt;br /&gt;
		ChildInSamePackage childInSamePackage = new ChildInSamePackage();&lt;br /&gt;
		childInSamePackage.setDefaultVariable(1);&lt;br /&gt;
		childInSamePackage.setProtectedVariable(5);&lt;br /&gt;
		childInSamePackage.setPublicVariable(7);&lt;br /&gt;
		//childInSamePackage.setPrivateVariable(7); // not allowed. Compile time Error&lt;br /&gt;
		&lt;br /&gt;
		Parent parent =new Parent();&lt;br /&gt;
		parent.setDefaultVariable(0);&lt;br /&gt;
		parent.setProtectedVariable(1);&lt;br /&gt;
		parent.setPublicVariable(0);&lt;br /&gt;
		//parent.setPrivateVariable(0); //not allowed. compile time error&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
package package1;&lt;br /&gt;
&lt;br /&gt;
public class ClassInSamePackage {&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Public, Protected and Default available but only to parent class object.&lt;br /&gt;
	 */&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		// TODO Auto-generated method stub&lt;br /&gt;
		Parent parent = new Parent();&lt;br /&gt;
		parent.setDefaultVariable(0);&lt;br /&gt;
		parent.setProtectedVariable(5);&lt;br /&gt;
		parent.setPublicVariable(9);&lt;br /&gt;
		//parent.setPrivateVariable(9); //not allowed&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
package package2;&lt;br /&gt;
&lt;br /&gt;
import package1.Parent;&lt;br /&gt;
&lt;br /&gt;
public class ChildInDifferentPackage extends Parent{&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Both public and Protected members can be accessed. &lt;br /&gt;
	 * But Protected can be accessed only through inheritance.&lt;br /&gt;
	 */&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		// TODO Auto-generated method stub&lt;br /&gt;
		ChildInDifferentPackage childInDifferentPackage = new ChildInDifferentPackage();&lt;br /&gt;
		childInDifferentPackage.setProtectedVariable(5);&lt;br /&gt;
		childInDifferentPackage.setPublicVariable(4);&lt;br /&gt;
		&lt;br /&gt;
		Parent parent = new Parent();&lt;br /&gt;
		parent.setPublicVariable(7);&lt;br /&gt;
		//parent.setProtectedVariable(7); // not allowed. protected members are available only through inheritance.&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
package package2;&lt;br /&gt;
&lt;br /&gt;
import package1.Parent;&lt;br /&gt;
&lt;br /&gt;
public class ClassInDifferentPackage {&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Only public members of parent class can be accessed &lt;br /&gt;
	 * but using the object of parent class.  &lt;br /&gt;
	 */&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
				&lt;br /&gt;
		Parent parent = new Parent();&lt;br /&gt;
		parent.setPublicVariable(4); // only public members are available.&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Access Control in C#==&lt;br /&gt;
The accessibility levels of the types (class, interface, struct, enum etc.) and the members, controls whether they can be used from the other code in our assembly (*) or other assemblies.&lt;br /&gt;
&lt;br /&gt;
===Accessibility levels provided by C#===&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Public&amp;quot;: It can be used for the types and type members. It allows them to be accessed everywhere in the same assembly or the other assembly.&lt;br /&gt;
*&amp;quot;Private&amp;quot;: It can be used only for the members. Private members are accessible only within the class or the struct in which they are declared. &lt;br /&gt;
*&amp;quot;Protected&amp;quot;: It is a member access modifier. It allows the class to hide its members from the other classes except the child classes. &lt;br /&gt;
*&amp;quot;Internal&amp;quot;: It can be used both for the types and type members. Internal types and members are accessible only by the code in the same assembly.&lt;br /&gt;
*&amp;quot;Protected Internal&amp;quot;: It’s a combination of protected and internal modifier. It allows the types and the members to be used by any code in the same assembly or by any derived class in another assembly. A protected internal element can be accessed in a derived class in another assembly by using an instance of the derived class.&lt;br /&gt;
[[File:Access2.png|400px|center|upright=0.56]]&lt;br /&gt;
&lt;br /&gt;
Usage of these access modifiers for all types or members is dependent on the conditions like the accessibility of the container restricts the accessibility of a type member. &lt;br /&gt;
*- Assembly is a physical unit that can be executed, deployed, versioned and secured.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                                &lt;br /&gt;
    internal class InternalClass&lt;br /&gt;
    {&lt;br /&gt;
        private int privateMember1;&lt;br /&gt;
        protected int protectedMember2;&lt;br /&gt;
        internal int internalMember3;&lt;br /&gt;
        protected internal int protectedInternalMember4;&lt;br /&gt;
&lt;br /&gt;
        private class NestedClass&lt;br /&gt;
        {&lt;br /&gt;
            void Test(Class1 foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.Member1 = 0; //OK&lt;br /&gt;
                foo.Member2 = 0; //OK&lt;br /&gt;
                foo.Member3 = 0; //OK&lt;br /&gt;
                foo.Member4 = 0; //OK&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public class PublicClass&lt;br /&gt;
    {&lt;br /&gt;
        private void Method1()&lt;br /&gt;
        {&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        private int privateMember1;&lt;br /&gt;
&lt;br /&gt;
        protected int Member1&lt;br /&gt;
        {&lt;br /&gt;
            get&lt;br /&gt;
            {&lt;br /&gt;
                return privateMember1;&lt;br /&gt;
            }&lt;br /&gt;
            set&lt;br /&gt;
            {&lt;br /&gt;
                Method1();&lt;br /&gt;
                privateMember1 = value;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        internal int internalMember2;&lt;br /&gt;
        public int publicMember3;&lt;br /&gt;
        protected internal int protectedInternalMember4;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class Test&lt;br /&gt;
    {&lt;br /&gt;
        void Test1()&lt;br /&gt;
        {&lt;br /&gt;
            InternalClass internalClass = new InternalClass(); //OK&lt;br /&gt;
            internalClass.privateMember1 = 0; //Compilation error here&lt;br /&gt;
            internalClass.protectedMember2 = 0; //Compilation error here&lt;br /&gt;
            internalClass.internalMember3 = 0; //OK&lt;br /&gt;
            internalClass.protectedInternalMember4 = 0; //OK&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    internal class DerivedClass : InternalClass&lt;br /&gt;
    {&lt;br /&gt;
        private void Test2()&lt;br /&gt;
        {&lt;br /&gt;
            privateMember1 = 0; //Compilation error here&lt;br /&gt;
            protectedMember2 = 0; //OK&lt;br /&gt;
            internalMember3 = 0; //OK&lt;br /&gt;
            protectedInternalMember4 = 0; //OK&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        private class NestedClass&lt;br /&gt;
        {&lt;br /&gt;
            void Test(DerivedClass foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.privateMember1 = 0; //Compilation error here&lt;br /&gt;
                foo.protectedMember2 = 0; //OK&lt;br /&gt;
                foo.internalMember3 = 0; //OK&lt;br /&gt;
                foo.protectedInternalMember4 = 0; //OK&lt;br /&gt;
            }&lt;br /&gt;
            void Test(Class1 foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.privateMember1 = 0; //Compilation error here&lt;br /&gt;
                foo.protectedMember2 = 0; //Compilation error here&lt;br /&gt;
                foo.internalMember3 = 0; //OK&lt;br /&gt;
                foo.protectedInternalMember4 = 0; //OK&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
------------------    Class in a different assembly     -------------------------------&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Test&lt;br /&gt;
    {&lt;br /&gt;
        void Test1()&lt;br /&gt;
        {&lt;br /&gt;
            InternalClass internalClass = new InternalClass(); //Compilation error here&lt;br /&gt;
&lt;br /&gt;
            PublicClass publicClass = new PublicClass; //OK&lt;br /&gt;
            publicClass.Method1(); //Compilation error here&lt;br /&gt;
            publicClass.privateMember1 = 0; //Compilation error here&lt;br /&gt;
            publicClass.Member1 = 0; //Compilation error here&lt;br /&gt;
            publicClass.internalMember2 = 0; //Compilation error here&lt;br /&gt;
            publicClass.publicMember3 = 0; //OK&lt;br /&gt;
            publicClass.protectedInternalMember4 = 0; //Compilation error here&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    internal class DerivedClass : PublicClass&lt;br /&gt;
    {&lt;br /&gt;
        private void Test2()&lt;br /&gt;
        {&lt;br /&gt;
            Member1 = 0; //OK&lt;br /&gt;
            internalMember2 = 0; //Compilation error here&lt;br /&gt;
            publicMember3 = 0; //OK&lt;br /&gt;
            protectedInternalMember4 = 0; //OK&lt;br /&gt;
        }&lt;br /&gt;
        private class NestedClass&lt;br /&gt;
        {&lt;br /&gt;
            void Test(DerivedClass foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.Member1 = 0; //Compilation error here&lt;br /&gt;
                foo.internalMember2 = 0; //Compilation error here&lt;br /&gt;
                foo.publicMember3 = 0; //OK&lt;br /&gt;
                foo.protectedInternalMember4 = 0; //OK&lt;br /&gt;
            }&lt;br /&gt;
            void Test(Class2 foo)&lt;br /&gt;
            {&lt;br /&gt;
                foo.Member1 = 0; //Compilation error here&lt;br /&gt;
                foo.internalMember2 = 0; //Compilation error here&lt;br /&gt;
                foo.publicMember3 = 0; //OK&lt;br /&gt;
                foo.protectedInternalMember4 = 0; //Compilation error here&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Conclusion==&lt;br /&gt;
The philosophies of access controls in object oriented languages have not changed much over time but certainly the access control across different Object Oriented Languages are different if not completely.   &lt;br /&gt;
 &lt;br /&gt;
==References==&lt;br /&gt;
# http://ruby-doc.org/core/&lt;br /&gt;
# http://rubylearning.com/satishtalim/ruby_access_control.html&lt;br /&gt;
# http://phrogz.net/programmingruby/tut_classes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/kktasw36(v=VS.80).aspx&lt;br /&gt;
# http://www.kuzbass.ru:8086/docs/isocpp/access.html&lt;br /&gt;
# http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr130.htm&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx&lt;br /&gt;
# http://stackoverflow.com/questions/2084801/c-using-declaration-scope-and-access-control&lt;/div&gt;</summary>
		<author><name>Amaroo</name></author>
	</entry>
</feed>