Pages

Thursday, 23 January 2014

rails rspec stub system call

I've tried everything to make this work. Here is my problem


if system("command")
  -- do something --
else
  -- do something else --
end

I obviously wanted to stub the system command. For that you can do something like this in your rspec file:

module Kernel

  def system(cmd)
    return true
  end

end

Thursday, 16 May 2013

rspec rails have_link, have_css

Found this extremely useful. If you want to test helpers or css code use this:

CREDITS TO


#Model
 
@user.should have(1).error_on(:username) # Checks whether there is an error in username
@user.errors[:username].should include("can't be blank") # check for the error message
 
#Rendering
response.should render_template(:index)
 
#Redirecting
response.should redirect_to(movies_path)
 
#Capybara Matchers
 
response.body.should have_content("Hello world")
response.body.should have_no_content("Hello world")
 
response.body.should have_css("input#movie_title")
response.body.should have_css("input#movie_title", :value => "Twelve Angry Men")
response.body.should have_css("input", :count => 3) #True if there are 3 input tags in response
response.body.should have_css("input", :maximum => 3) # True if there or fewer or equal to 3 input tags
response.body.should have_css("input", :minimum => 3) # True if there are minimum of 3 input tags
response.body.should have_css("input", :between => 1..3) # True if there 1 to 3 input tags
response.body.should have_css("p a", :text => "hello") # True if there is a anchor tag with text hello
response.body.should have_css("p a", :text => /[hH]ello(.+)/i)
# True if there is a anchor tag with text matching regex
 
response.body.should have_xpath("//a")
response.body.should have_xpath("//a",:href => "google.com")
response.body.should have_xpath("//a[@href => 'google.com']")
response.body.should have_xpath("//a[contains(.,'some string')]")
response.body.should have_xpath("//p//a", :text => /re[dab]i/i, :count => 1)
 
 
# can take both xpath and css as input and can take arguments similar to both have_css and have_xpath
response.body.should have_selector(:xpath, "//p/h1")
response.body.should have_selector(:css, "p a#movie_edit_path")
 
# For making capybara to take css as default selector
Capybara.default_selector = :css
response.body.should have_selector("input") #checks for the presence of the input tag
response.body.should have_selector("input", :value =>"Twelve Angry Men") # checks for input tag with value
response.body.should have_no_selector("input")
 
# For making capybara to take css as default selector
Capybara.default_selector = :xpath
response.body.should have_selector("//input") #checks for the presence of the input tag
response.body.should have_selector("//input", :value =>"Twelve Angry Men") # checks for input tag with value
 
 
# To access elements inside form
response.body.should have_field("FirstName") # checks for presence of a input field named FirstName in a form
response.body.should have_field("FirstName", :value => "Rambo")
response.body.should have_field("FirstName", :with => "Rambo")
 
response.body.should have_link("Foo")
response.body.should have_link("Foo", :href=>"googl.com")
response.body.should have_no_link("Foo", :href=>"google.com")

Friday, 19 April 2013

rails stub chained for rails activerecord::relations

For whenever you have a multiple clauses on a model like:

Product.where(condition).includes(tables)

you can stub this using this

Product.stub_chain(:where, :includes).and_return([what you want])

easy :)

http://apidock.com/rspec/Spec/Mocks/Methods/stub_chain

Monday, 11 February 2013

just a quick word on indexes

CREDIT TO:

https://tomafro.net/2009/08/using-indexes-in-rails-index-your-associations

Many rails developers are great at building applications but have limited experience in database design. As a consequence, projects often have half-baked indexing strategies, and as a result suffer bad performance.
To try and improve this I’ve planned a series of posts on indexes, targetted at rails developers. In this first post I’ll introduce indexes and how to index your associations, then I’ll write about choosing additional indexes to improve query performance, and finally how to avoid redundant and duplicate indexes.

A brief overview of database indexes

Wikipedia states that ‘a database index is a data structure that improves the speed of operations on a database table’. Unfortunately, this improvement comes at a cost.
For every index on a table, there is a penalty both when inserting and updating rows. Indexes also take up space on disk and in memory, which can affect the efficiency of queries. Finally, having too many indexes can cause databases to choose between them poorly, actually harming performance rather than improving it.
So while indexing is important, we shouldn’t just throw indexes at our slow queries: we need to choose carefully how to index our data.

Indexing simple associations

By far the most common performance problem I’ve encountered in rails projects is a lack of indexes on foreign keys. There’s no real excuse for this – not indexing foreign keys can cripple your app.

Monday, 21 January 2013

ruby/rails csv skip first row

I've been looking for a solution to this problem for ages now.

This question kept popping up when i was searching for how to skip the first line with the CSV / FasterCSV libraries, so here's the solution that if you end up here.
the solution is... CSV.foreach("path/to/file.csv",{:headers=>:first_row}) do |row|
HTH.
CREDIT TO:
http://stackoverflow.com/a/7727809

Wednesday, 21 November 2012

android sdk on mac osx

so i started with this tutorial :

http://buildcontext.com/blog/2009/installing-android-sdk-browser-testing-mac-os-x

what you really want is here:

http://stackoverflow.com/a/7962765


If you are getting the error XML verification failed for https://dl-ssl.google.com/android/repository/repository.xml. Error: cvc-elt.1: Cannot find the declaration of element 'sdk:sdk- repository'. Failed to fetch URL
in mac os, then download android sdk (Mac OS X (intel) android-sdk_r15-macosx.zip) from http://developer.android.com/sdk/index.html. Unzip this package and copy this unzip package on your mac root directory (Macintosh HD) and rename this unzip package as android-sdk. Now open android-sdk->tools->android. The sdk update will start automatically. You dont need to install java 6. It is automatically installed in your mac os.

Monday, 19 November 2012

ruby regex expressions

While working with regex expression i found two values resources:


and this cheat sheet for regular expressions