test driven ruby

34

Upload: nikola-duza

Post on 21-Jan-2018

115 views

Category:

Software


0 download

TRANSCRIPT

testdrivenr u b y

Yukihiro “Matz” MatsumotoRuby creator

Ruby is...● programming language● dynamic ● open source ● focus on simplicity and productivity ● it has an elegant syntax that is natural to read and easy to write

● appeared in 1995.

● Became popular in 2005. with the apperance of Ruby on Rails (web application framework written in Ruby)

OK, lets see some Ruby code!

NO.

Of course, Ruby wouldn’t be Ruby without tests.

test driven development - tdd

add a test

run all tests and see if the new one fails

write some code

run tests

refactor code

-repeat-

fail

pass

software development methodology:

the TDD circle of life

TEST FAILS

TEST PASSES:“IT’S ALL GREEN”

refactor

refactor

refactor

refactor

refactor

RSpec

TDD framework / test tool for Ruby.

Domain specific language.

Aims to resemble specifications in natural language.

TIME FOR RSPEC SETUP

1. Add RSpec gem (package) to your project

2. Write ‘specs’ (tests / specifications) 3. Run them with rspec

4. Observe the reports of the specs crashing.

5. Proceed with the TDD cycle.

now, we finally switch over to...

examples!

First things first, breakfast.

String breakfast = null;

if (breakfast == null) {

breakfast = "burek";

}

breakfast = nil

if breakfast == nil

breakfast = "burek"

end

String breakfast = null;

if (breakfast == null) {

breakfast = "burek";

}

if breakfast.nil?

breakfast = "burek"

end

String breakfast = null;

if (breakfast == null) {

breakfast = "burek";

}

if !breakfast

breakfast = "burek"

end

String breakfast = null;

if (breakfast == null) {

breakfast = "burek";

}

unless breakfast # unless = if not

breakfast = "burek"

end

String breakfast = null;

if (breakfast == null) {

breakfast = "burek";

}

breakfast = "burek" unless breakfast

String breakfast = null;

if (breakfast == null) {

breakfast = "burek";

}

breakfast ||= "burek"

Pretty neat, huh? There’s more.

public void greetSomeone(String name) {

return "Hello there, " + name;

}

def greet_someone(name)

"Hello there, " + name

end

public void greetSomeone(String name) {

return "Hello there, " + name;

}

def greet_someone(name)

"Hello there, #{name}"

end

Now the loops.

for i in 0..10

puts i

end

for(int i = 0; i < 11; i++) {

System.out.println(i);

}

10.times {|i| puts i}

for(int i = 0; i < 11; i++) {

System.out.println(i);

}

Iterating through an array.

String[] people = {"Bob", "Billy", "Jim"};

int i = 0;

while(i < people.length) {

System.out.println(people[i]);

i++;

}

people = ["Bob", "Billy", "Jim"]

i = 0

while i < people.size

puts people[i]

i += 1

end

String[] people = {"Bob", "Billy", "Jim"};

for(int i = 0; i < people.length; i++) {

System.out.println(people[i]);

}

people = ["Bob", "Billy", "Jim"]

for i in 0..people.size

puts people[i]

end

String[] people = {"Bob", "Billy", "Jim"};

for(String person: people) {

System.out.println(person);

}

people = ["Bob", "Billy", "Jim"]

people.each { |person| puts person }

Lets see if this actually works.

end_presentation unless questions?