Sunday, February 23, 2014

Behavior Driven Development

Testing - Behavior Driven Development

BDD vs. TDD


The trouble with writing TDD testing is that too many developers focus on “How” when writing their unit tests, so they end up with tests that only confirm what the system does. It is really a feature driven approach to TDD. Some even say that BDD is just TDD done correctly.


BDD focuses on telling stories.


Side Note:

Unit Tests
- Tests that a relatively small piece of code is doing what is is intended to do.
- These follow the white box method and testing the inputs and the outputs of a test.


Integration Test
- This is done to demonstrate that different pieces of the system work together. Integration tests cover whole applications and require much less effort to implement.


Cucumber


- Does not actually write code, but it provides syntax and context for them to actually be  written.


Feature: Manage Articles
 In order to make a blog
 As an Author
 I want to create and manage articles


Scenario: Articles List
 Given I have articles title Pizza, Breadsticks
 When I go to the list of articles
 Then I should see “Piazza
 And I should see “Breadsticks”


Capybara - Simulate Users





Capybara is used for high level tests in addition to Test Driven Development to simulate how a user interacts with an application.


- It Clicks on things.


The DSL (Domain Specific Language)


Navigation  - visit(‘/home’)


Click Links and Buttons - click_link(‘id-of-link’)


Interacting with Forms - fill_in(‘First Name’, :with => ‘Thomas)


Querying - page.has_selector(‘table tr’)


Finding - find_link(‘hello’).visible?


Scoping
within(‘li#movie’) do
 fill_in ‘Name’, :with => ‘Jimmy’
end


Scripting (JavaScript)
page.execute_script(“$(‘body’).empty()”)


Ajax
click_link(‘foo’)
click_link(‘bar’)
page.should have_content(‘baz’)


The default wait time is 2 seconds between requests, but it can be changed.
Capybara.default_wait_time = 5

Monday, February 10, 2014

Default Arguments

I want access to my variables.

There are a number of ways to pass in variables into ruby methods. After my first week at The Flatiron School I initially struggled with passing variable parameters into a function. Within this blog post I will introduce the different ways to pass in variables to a functions.

Required Arguments

def my_method(a, b)
  puts a
  puts b
end

my_method(25, "hello world")
 # => 25
 # => "hello world"

This method may only be called if both a & b are passed in.

my_method(25)
# => "wrong number of arguments (1 for 2)"

If just pass in one method is passed in we will receive the error "wrong number of arguments"

Default Arguments

One to avoid this issue is to pass in a variable as a default argument.

def my_method(a, b="hello world")
  puts a
  puts b
end

In this example b has a default value of "hello, world". If only one variable is passed in this function, will still run.

my_method(25)
# => 25
# => "hello world

Within Other Functions

A third way of getting variables into a function is by saving the variables into another function. I found this useful, if I didn't want to add the same arguments every time.

def add_variable
  hash1 = { :name => thomas, :email => surgentt@gmail.com }
end

def my_method
  add_varable
end

In this example I gain access to the variable hash1 without actually passing it into the function via an argument.