Sunday 31 August 2014

Simple Step by Step testing in Ruby on Rails Using RSpec

Straight to the point.
open up terminal and create a new Rails project, simple_test:

    $ rails new simple_test --skip-test-unit
    $ cd simple_test

option --skip-test-unit, tells Rails not to generate a test directory associated with Test::Unit framework. this is because we want to use another testing framework named RSpec to write our tests.

then we need to include RSpec Gem in 'Gemfile' in your project root directory

    group :development, :test do
      gem 'sqlite3', '1.3.8'
      gem 'rspec-rails', '2.13.1'
    end

    group :test do
      gem 'selenium-webdriver', '2.35.1'
      gem 'capybara', '2.1.0'
    end
  
This includes rspec-rails in a development environment so that we have access to RSpec-specific generators, and it includes it in test mode in order to run the tests. We don’t have to install RSpec itself because it is a dependency of rspec-rails and will thus be installed automatically. We also include the Capybara gem, which allows us to simulate a user’s interaction with the sample application using a natural English-like syntax, together with Selenium, one of Capybara’s dependencies.

    $ bundle install

it might be better to update the gems with bundle update to make sure the versions match, and then run bundle install to make sure the Gemfile.lock file is fully up-to-date.
Next, we need to configure Rails to use RSpec in place of Test::Unit.

    $rails generate rspec:install

you should see:

    create  .rspec
    create  spec
    create  spec/spec_helper.rb
if you're using sublimeText, you can open the entire project by navigating to your project directory and then

    $ subl .

or any other editor.
ok, now we set to start developing. since we're following MVC pattern here, I jut give an short overview about that. model is your data access layer. view is your presentation layer, and controller is your business logic layer. business layer i.e. controllers, are between those 2 other layers.
it means, if some data needs to be shown by presentation layer, i.e. views, it can't access the model (data access layer, or database side) by itself. it needs to go through the business layer i.e. controllers.
now if you opened the entire project in your editor, and then go into 'app' folder in project root directory you'll see folders related to these layers, models, views, controller. there are some convention here which makes the development easier, which you can search it for yourself.
now lets make a controller:

    rails generate controller StaticPages home help --no-test-framework

--no-test-framework option suppresses the generation of the default RSpec tests, which we won’t be using. Instead, we’ll create the tests by hand. this will generates a controller, StaticPages, with 2 action methods : home and help, which both represent 2 pages(look into app/view/static_pages). now, since i just want to show how TDD works, i don't go into details on how writing tests for the existing code. instead suppose i want to add another page, About.
In test-driven development, we first write a failing test, represented in many testing tools by the color red. We then implement code to get the test to pass, represented by the color green. Finally, if necessary, we refactor the code, changing its form (by eliminating duplication, for example) without changing its function. This cycle is known as “Red, Green, Refactor”.
because here, i'll do integration testing we need to run following command:

    $ rails generate integration_test static_pages
      invoke  rspec
      create    spec/requests/static_pages_spec.rb

This creates the static_pages_spec.rb in the spec/requests directory. As with most generated code to use the Capybara you need to add the following line in  spec/spec_helper.rb

    .
    .
    .
    RSpec.configure do |config|
      .
      .
      .
      config.include Capybara::DSL
    end

checkpoint: now we have a controller, static_pages_controller, it has 2 action methods, home, help and we want to add another one, about. we have RSpec installed, and we execute the rails generate integration_test static_pages, which generated spec/requests/static_pages_spec.rb for us. we put our test inside that file.

Actual TDD

We’ll start the testing cycle by writing a failing test for the About page.

    in spec/requests/static_pages_spec.rb

    require 'spec_helper'

    describe "Static pages" do
      describe "About page" do

        it "should have the content 'About Us'" do
          visit '/static_pages/about'
          expect(page).to have_content('About Us')
        end
      end
     end

if you run the RSpec using

    $ bundle exec rspec spec/requests/static_pages_spec.rb

the output is very informative. it includes:

    No route matches [GET] "/static_pages/about"

this means we need to add /static_pages/about to routes file in config/routes.rb

    get "static_pages/about"

now running the test again

    $ bundle exec rspec spec/requests/static_pages_spec.rb

complains that

    The action 'about' could not be found for StaticPagesController

go to app/controllers/static_pages_controller.rb and add action for about, same as home and help actions
      
    def about
    end

now running the test again

    $ bundle exec rspec spec/requests/static_pages_spec.rb

says that we are missing a “template”, i.e., a view:

    Missing template static_pages/about

To solve this issue, we add the about view. This involves creating a new file called about.html.erb in the app/views/static_pages directory with following content:   

<h1>About Us </h1>

Running the test again

    $ bundle exec rspec spec/requests/static_pages_spec.rb

it should show us the green.
to see the page you created, run the rails server in terminal:

    $rails server

then in your browser navigate to

    0.0.0.0:3000/static_pages/about

and you should see the about view you created.
now suppose we want to write test to check the page title. we want the "About page" has the title "About Us". we write the test first:

    it "should have the title 'About Us'" do
      visit '/static_pages/about'
      expect(page).to have_title("About Us")
    end

if you run the test

    $ bundle exec rspec spec/requests/static_pages_spec.rb

it should fail

    expected #has_title?("About Us")

The test uses the *have_title* method, which checks for an HTML title with the given content. In other words, the code

    expect(page).to have_title("About Us")

to pass the test you just need to change the title of about page to "Abut Us" in its relevant view file in app/views folder, which is easy

some side notes: 
these tests are technically integration tests, however writing unit tests are similar. you just test the data based on your model, for example for invalid data, or password length. etc.
  • I tried to be more detailed and descriptive, sorry if that's so long.
  • all the steps have been tested on Kubuntu 14.04
  • this was adopted mostly from http://www.railstutorial.org/
  • if you see any errors, leave a comment please 

Wednesday 9 July 2014

How to Install genymotion on Kubuntu14.4

you know what the genymotion is. simply, it's emulator to run android app.
straight to the point:

1- Install virtualbox. the easiest way is to install it via Software Center. just search for it and then install
you have other options: navigate to VirtualBox  website, find your system installer package then install it, simple
or you can use 'wget' command.

2- install Genymotion. you need to sign up to be able to download, which is free. to install genymotion open up terminal(CTRL+T) then run the following commands:
  • you are in your home directory: cd Dowloands
  • give executable permission to installation file. you might have different version: chmod +x genymotion-2.2.2_x64.bin
  • run the installer:  ./genymotion-2.2.2_x64.bin
  • move the directory to your home directory:                                                mv geymotion ~/.genymotion
  • add this executable path to make your life easier:  echo 'export PATH="/home/$USER/.genymotion:$PATH"' >> /.bashrc                       you don't have to add this but then you have to go to its directory and run it from there.
now you can simply run it by : genymotion

I had some problems after installation:
  1. Error while opening log file: /home//.Genymobile/genymotion.log what i did to solve this problem was that i created a hidden folder in the directory in error message and then just create a log file with that name. you need to replace the with your own user name
      • cd home/
      • mkdir .Genymobile
      • cd .Genymobile
      • touch genymotion.log  
     
  2. Cannot mix incompatible Qt library i searched a lot to solve this problem, there were many solutions but the one actually worked was that just try to remove everyfile in genymotion instalation directory that are related to qt. you jut need to find files in every folder in installed directory with qt in their names and just delete them. that's it. now you can run genymotion by: genymotion  in terminal.
  3. after installation you can do whatever you want with it, it's simple and it's just configuring what you want and hitting next or ok.
Goodluck

Monday 30 June 2014

USYD - INFO3315 - Mid term exam questions - 2013

 Last year assignment and tutorials was about FitBit. every students got one of this wireless FitBit device and every tutorial, assignment and quiz were around that. they might change it this year but it gives you overall view on how it looks like or what they assess in exam. i also wanted to put my assignment here but it's too long and I think you'll do better.
here's the questions:

Question 1(10 marks) 

(2 marks) What is one important affordance of the FitBit?

(2 marks) For your project this semester, you aim to create an interface that enables people to exploit their FitBit data so they can observe changes in their inactivity over time. Explain how the affordance of the FitBit are useful for this?

(2 marks) Suppose we had the perfect sensor for the project goal, state one key affordance would it have that the FitBit does not have.

(2 marks) Explain how  think aloud evaluation has the potential to give insight into the user's mental model?

Question 2(10 marks) 

(6 marks) For the project this semester, a friend drafted the following single sentence statement to describe the goal of their interface:
  • A sedentary worker ill be able to determine the average number of minutes per work-day that they were inactive during work-hours over the last full calendar month.
 There is missing key elements of the single sentence statement. What are the missing elements?

To help your friend understand how to improve their single sentence statement, amend the following copy, writing on an example of each missing part.
  •  A sedentary worker will be able to determine the average number of minutes per work-day that they were inactive during work-hours over the last full calendar month.

(2 marks) In the week 4 lab you saw the following question based on a standard questionnaire:
  • During the last 7 days, how much time in total did this user usually spend sitting on a week day?      ---------------hours ---------------- minutes
In light of your lecture on design of questionnaire, what is a serious weakness of this question?

(2 marks) The week 5 reading, Lewis et al(2013) UMUX-LITE when there's no time for SUS, provided evidence of the value of just 2 usability questions, rather than the full SUS. what is the main reason to use SUS, even though it takes the user longer to answer?

Question 3(10 marks) 

In the week 4 lb you studied the screen shot below, from the FitBit interface. It shows activity of a hypothetical user called Alex. The user has their mouse over the bar for the time 11.15 - 11.30am. If the user moves their mouse away from the bars, the bubble disappears.



Suppose you are designing the tasks for a think aloud usability evaluation, to access whether users can make effective use of this interface to access their level of activity

(4 marks) State one important weakness in the following task:
  • Use your mouse to hover over the tallest bar in the chart to see how many steps this user took at that particular time?

(2 marks) Write a better version of the question that avoids this weakness.

(2 marks)  State what Fitts' Law predicts about the effect of the size of the click-target where the target is 5 centimetres  from the current position of the mouse cursor.
  
(2 marks) In a desktop GMOS analysis, P ranges from 0.8 to 1.5, according to Fitts' Law. Is this directly applicable to a tabletop touch interface?
    Yes/ No
Justify your answer in terms of the relationship between Fitts' Law and the way it relates to the value of P in a GOMS analysis.

Question 4 (10 marks)

In the space below, draw a concept map. it should answer the question:
  • What are the characteristics of Think-Aloud usability evaluation?
Use only the concepts and links listed below.(They are in alphabetic order)
Use just 7 of the most important concepts to create just 6 of the most important propositions.

Concept list
efficiency
expert-users
learnability
memorability
monitoring
no-user method
novice-users
qualitative-method
quantitative-method
satisfaction
think-aloud
user method
user-errors

Link names:
evaluates
is-a(n)
is-important-for
is-suited-to

p.s: course website INFO3315 ... they changed it a lot
       please don't ask for answer. I even didn't have the questions

Wednesday 11 June 2014

How to insert code snippet into your blog...easy way

ok, recently I was trying to copy paste some code into my blog. i tried different methods, and i found this method explaining Here the easiest and efficient way. you have other options if you don't like the logo at the bottom of your code snippet, which is just adding JavaScript and CSS files to your template. the only reason I used that method mentioned in that blog was that it gave me what I wanted and I didn't mind the logo, plus it was straight forward

Break out game in C++ using QT 5.2.1 - Stage 1 - Header files

In his stage we were required to implement a bouncing ball inside a box. ball needs to bounce of the box edges at constant speed. singleton design pattern used here. this is done in QT. we have header and implementation files for coordinate, ball, and dialog classes, in addition to main method.
here is the coordinate header file...coordinate.h...

ball.h
dialog.h

sample config file looks like this

now some explanation. coordinate class is for translating coordinate in Qt in a way that we are comfortable of. it means Qt consider top left corner (0,0), and while you're moving to right or down it increases the x and y coordinate. personally, I think we don't need this..well...here it is... ball class has some functions needs proper implementation beside normal getter/setter methods. render and isCollision functions. isCollision function checks if the ball hit any edges of the box, and render function handle the ball movements. dialog class is the core of this to put them all together. we used Singleton Design Pattern here to make sure only one instance of dialog exists at any time. I hope I explained it good enough, as I'm still learning. I'll post the implementation files and main method in near future, meanwhile stay hungry, keep coding

Some other information:

  • this is not my code. mine was similar but it didn't changed colour when ball hits the box
  • again this is implemented in Qt
  • the actual website that have the tutorial on basic set up to start on this assignment can be found Here

Friday 6 June 2014

Break out game in C++ using QT 5.2.1 - Introduction

I had an assignment this semester which was about to create BreakOut game using C++ and Qt IDE. it was part of INFO3220 - Object Oriented Design subject which is offered in prestigious university of Sydney...where recently its e-learning website known as BlackBoard crashed....assignment was in 3 stages and it needed to be done individually.
stages are:
  1. bouncing ball inside a box in a constant speed, and specification of ball and box needs to be read from a file.
  2. lay out bricks with initial health. ball should hit them and based on the bricks health, it should behave accordingly, either bounce back or remove brick.
  3. add paddle at the bottom, ball needs to bounce back from that, and paddle needs to be controlled either by mouse or keyboard. shows players life and score.
I post the source code little by little, and I'll do my best to explain them. the code which I will use is not mine. we implemented the first stage then we for later stages we got a sample code for former stage and developed that to add additional features. the sole purpose of this is to give students the chance to have sample to work out and see how they need to do stuff. I'm not seeking no any intention whatsoever to get benefit from that. I thought about considering copyright of the person who wrote the code, I'm not sure if this cause any problem or any ethic issue. All rights reserved for INFO3220 teaching staff.
I'll post the code as soon as I get time...considering exams are coming...

I also try to put rest of assignments and task for other subjects hopefully. this might take time to organise them, so stay tuned
Pamador out

Saturday 31 May 2014

How to install Windows application under Ubuntu

If you want to install a program and there is not the Linux/Ubuntu version of that program, you need to install a program named 'Wine' in Ubuntu. To install Wine you have two options:
1- in software centre just type 'Wine' and install it.
2- search in the internet and install that program


University of Sydney Elearning is Down!!!

apparently last night university of Sydney E learning website known as Blackboard has been crashed and all the unit of studies of all student has been removed. students can no longer see their enrolled unit of studies. and the bad thing is students are in final week, which is week 13, and they're working hard on assignments to submit them on due date, which put pressure on them. this might be good for students, so they probably get more time for assignments, or not. students can no longer see, lecture notes, tutorials, lecture recording etc. this is not good for prestigious university of Sydney.
I can see all the funny thing UTS, UNSW even UWS are saying about that. you let us down you damn inefficient, insecure, badly design, slow Backboard. you just list all the software quality attributes, and see none but one about that website which is University of Sydney shining logo.

Saturday 8 March 2014

Windows 8.1 and Kubuntu 13.10 dual boot

I have following specification for my system:
Dell Xps laptop without pre-installed Windows 8, 4GB RAM, 128 GB HDD, no DVD drive

You need to get Windows 8.1 installation file...genius...
you have couple of options, to get that. i assumed you could do that part, if not please see our ghinj customer service.

I have Windows 8.1 ISO file. if you want to install from USB memory stick, you need Windows 7 USB DVD Download Tool, from Microsoft website. this tool is easy to use you just follow the steps and it creates a bootable memory stick for you. 

On the other hand, if you don't have USB memory stick you have other option. in my case, i have a external hard drive which connects to USB 2 or 3 port by  cable. 

connect your external hard drive to your machine then extract the Windows ISO file to that. 
then follow this steps to make it bootable:
1- to run command prompt---> type 'cmd' in search box then enter or 
    windows logo key+ r ---> cmd
2- navigate to your external hard dive ---> e.g if your hard drive is H, the h:
3- then 'cd boot'
4- run this command: bootsect h:

then reboot your machine, and go through the Windows installation, which is pretty straight forward.

so far so easy.

now to install Kubuntu 13.10 or Ubuntu. first download it from Ubuntu or Kubuntu website.
installation file is not as big as Windows, so you can use usual USB memory stick.
there are tools to create a bootable memory stick for your installation, like Universal USB Installer, or Unetbootin, which i used unetbootin.
download and run Unetbootin. select the 'DiskImage', then browse to your image file and make sure the program has recognized your USB memory stick, hit 'OK' and let the program do its job.

when finished, do not reboot. there is one more step. you need to make some room for Ubuntu.
you need to run the 'Disk management' tool in Windows. 
quick way is -> win logo key+ r  -> diskmgmt.msc
or
right click on 'Computer' icon on your desktop, then choose 'Manage'. in the opened windows select 'Disk Management'. you can also go through control panel.
you may need administrator permission.
while inside disk management, right click on your windows drive and hit 'Shrink'. let it calculate the space then choose how much space you need then hit OK. you don't need to format that.

now put your bootable Ubuntu USB memory stick and reboot your machine. you need to make sure that your machine boot from USB first...check your boot menu for boot order...

if everything is fine you should see Ubuntu installation page. select install now. first a few steps are pretty straight forward like choosing your language, connecting to wireless network, etc. important part is partitioning your empty space on your hard drive. here we want to have dual boot so you should choose ' Something else' then it shows your hard drive lay out. you should be able to see the unpartitioned space in the list with other partitions. click on that, to create the partitions, here's what i created:
1- 250 Mb ..... EXT2 as /boot
2- 4096 Mb .... swap area ....depends on the size of installed RAM
3- rest of the space .... EXT4 journaling file system as /

for bootloader option, don't choose any drive, and leave it unchanged. you should have the format tick box for /boot and / drive. swap drive can't be formatted.

then go through the rest of installation. you need to reboot and you should see the GRUB menu which shows you the Windows and Ubuntu to choose. if not, then there are 3 situations here:
1- you go to Windows
2- you go to Ubuntu
3- or nothing 

i was lucky it worked for me in my first installation so i'm not going through how to fix those issues.

Windows secure boot, fast boot caused problem, and it needs to be turned off. or you go to Ubuntu then you need repair the boot loader, which can be done by installing boot repair software on Ubuntu and run that. it should fix the problem.

Good Luck 


p.s: couple of points:

 - Kubuntu and Ubuntu are same...almost. you can install KDE on Ubuntu or directly install Kubuntu.
 - i tested this steps and it worked for me.
 - i should have put some picture to make it more clear...blame me
 - i tried to be clear, if it wasn't, blame someone else
 - if you could follow these steps and then you see OS selection menu, then buy yourself a candy...:)
 - if you see an error, or if you know a better working way to achieve this, let the others know by sharing your experience.




How to install Viber on Kubuntu 13.10

you have 2 options to get the package
1- by using wget command 
or 
2- navigate to viber.com and download your suitable viber version.
after getting the package which is in .deb format....in my case...
open up the terminal then go to the directory you have downloaded viber then you must run :

sudo dpkg -i viber.deb

dpkg is debian package manager and -i switch install the package...check for other switches if you'd like. and that's it

p.s: if viber installation file is in zip format you need to unzip it then run the installer

How to configure shortcut key for Konsole on Kubuntu 13.10

There are couple of ways to do that, here I just give one of the many ways.
  1. right click on kick off application launcher
  2. choose 'Edit Application'
  3. in left hand side of the windows, select System, then Konsole(Terminal)
  4. when you select that, on the right hand side of the window, click on 'Advanced' tab
  5. at the bottom of that tab you can set whatever short cut key you like.
then save the changes and start using.
as I said there are other ways as well, however, I tried  this and it worked for me.