where you write a test before you write just enough production code to fulfill that test and refactoring.” http://www.agiledata.org/essays/tdd.html Test Driven Development www.ict4g.org
• Mailers • API After the registration, a new user should redirect to the index page. After logout, a user should see the message “Goodbye!”. www.ict4g.org In RoR we can test:
• Mailers • API If I write values such as “/home” or “/root” in the address bar of the browser, I should redirect to the homepage. www.ict4g.org In RoR we can test:
= users(:one) end ! test "should get new" do get :new assert_response :success end ! test "should create user" do assert_difference('User.count') do post :create, user: { email: @user.email, name: @user.name, surname: @user.surname } end assert_redirected_to user_path(assigns(:user)) end ! test "should show user" do get :show, id: @user assert_response :success end ! test "should get edit" do get :edit, id: @user assert_response :success end ! test "should update user" do put :update, id: @user, user: { email: @user.email, name: @user.name, surname: @user.surname } assert_redirected_to user_path(assigns(:user)) end ! test "should destroy user" do assert_difference('User.count', -1) do delete :destroy, id: @user end assert_redirected_to users_path end end www.ict4g.org one: name: MyString surname: MyString email: MyString ! two: name: MyString surname: MyString email: MyString test/fixtures/users.yml test/functional/users_controller_test.rb
save user without name" do user = User.new assert !user.save # refute user.save end end $ rake test test/unit/user_test.rb test_should_not_save_user_without_name F ! Finished tests in 0.044632s, 22.4054 tests/s, 22.4054 assertions/s. ! 1) Failure: test_should_not_save_user_without_name(UserTest) [test/unit/user_test.rb:6]: Failed assertion, no message given. ! 1 tests, 1 assertions, 1 failures, 0 errors, 0 skips 1 2 TDD Step by step www.ict4g.org
! it "Should create a user with name, surname, email and password" do u = FactoryGirl.create(:user) u.should be_valid end ! # WITHOUT SOME ATTRIBUTES ! it "Should not create a user without name" do u = FactoryGirl.build(:user, name: nil) u.should_not be_valid # assert !u.save or refute u.save end ! end Come back to RSpec www.ict4g.org spec/models/user_spec.rb
USER ! it "Should create a user with name, surname, email and password" do u = FactoryGirl.create(:user) u.should be_valid end ! # WITHOUT SOME ATTRIBUTES ! it "Should not create a user without name" do u = FactoryGirl.build(:user, name: nil) u.should_not be_valid end ! it "Should not create a user without surname" do u = FactoryGirl.build(:user, surname: nil) u.should_not be_valid end ! it "Should not create a user without username" do u = FactoryGirl.build(:user, username: nil) u.should_not be_valid end ! it "Should not create a user without email" do u = FactoryGirl.build(:user, email: nil) u.should_not be_valid end ! describe "Testing instance methods" do ! it "full_name method should returns full name" do user = FactoryGirl.build(:user) user.full_name.should eq [user.name, user.surname].join(" ") end end end www.ict4g.org
"Should not create a user without name" do u = FactoryGirl.build(:user, name: nil) u.should_not be_valid end ! it "Should not create a user without surname" do u = FactoryGirl.build(:user, surname: nil) u.should_not be_valid end ! it "Should not create a user without username" do u = FactoryGirl.build(:user, username: nil) u.should_not be_valid end ! it "Should not create a user without email" do u = FactoryGirl.build(:user, email: nil) u.should_not be_valid end end describe User do ! # WITHOUT SOME ATTRIBUTES %w(name surname username email).each do |attr| context "with a nil #{attr}" do it "should not create a valid user" do FactoryGirl.build(:user, attr => nil ).should_not be_valid end end end www.ict4g.org
! it "routes to #index" do get("/users").should route_to("users#index") end ! it "routes to #show" do get("/users/1").should route_to("users#show", :id => "1") end ! it "routes to #edit" do get("/users/1/edit").should route_to("users#edit", :id => "1") end ! it "routes to #create" do post("/users").should route_to("users#create") end ! it "routes to #update" do put("/users/1").should route_to("users#update", :id => "1") end ! it "routes to #destroy" do delete("/users/1").should route_to("users#destroy", :id => "1") end ! end ! end Test routes www.ict4g.org
=> '[email protected]', :password => 'password') end ! it "signs me in" do visit '/sessions/new' within("#session") do fill_in 'Login', :with => '[email protected]' fill_in 'Password', :with => 'password' end click_link 'Sign in' expect(page).to have_content 'Success' end ! end Capybara with RSpec www.ict4g.org
write whatever you want up until the first scenario, which starts with the word Scenario on a new line. ! Every scenario consists of a list of steps, which must start with one of the keywords Given, When, Then, But or And. Cucumber treats them all the same, but you shouldn’t. Features www.ict4g.org
food as supplier @javascript Scenario: successful login as a supplier Given there exists [email protected] And I am on the landing page When I sign in as [email protected] Then I should be on the home page And I should see "Offers" And I should see "New Offer" www.ict4g.org
as a supplier Given I am on the landing page When I sign in as … ! @javascript Scenario: successful sign in as a collector Given I am on the landing page When I sign in as … ! @javascript Scenario: successful sign in as a manager Given I am on the landing page When I sign in as … www.ict4g.org
the landing page ! @javascript Scenario: successful sign in as a supplier When I sign in as … ! @javascript Scenario: successful sign in as a collector When I sign in as … ! @javascript Scenario: successful sign in as a manager When I sign in as … www.ict4g.org
path_to(page_name) end ! When /^(?:|I )follow "([^"]*)"$/ do |link| click_link(link) end ! When /^(?:|I )fill in "([^"]*)" with "([^"]*)"$/ do |field, value| fill_in(field, :with => value) end ! # Examples: # # Given I am signed in as [email protected] # Given I am logged in as [email protected] # When I sign in as [email protected] # When I log in as [email protected] ! Given /^I (?:am signed in|sign in|am logged in|log in) as ([\w]+[\w-]*[\w\d](?:@example.com))$/ do |mail| @current_user = User.find_by_email(mail) @current_user.should_not be_nil ! steps %{ Given I am on the landing page When I follow "Login" within the menu And I fill in "Email" with "#{mail}" And I fill in "Password" with "password" Then I press "Sign in" within the modal popup } end Capybara gem Capybara gem Capybara gem RSpec gem www.ict4g.org