Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Increase test coverage #186

Merged
merged 3 commits into from

3 participants

@ArturG

More tests! More!

@PragTob
Collaborator

Hey looks good! You are on a roll man!

For most of the specs I don't get why there has to be a mac user agent background though - I mean for the dl I get it but for the rest... ?

@ArturG

Hi,

in our StaticController we had the following code:

def platform
  if Rails.env.test?
    "mac"
  else
    request.user_agent.match(/Mac|Linux|Windows/).try(:[], 0).try(:downcase)
  end
end

after refactoring we got the following:

def platform
  request.user_agent.downcase
end

So we had to define User-Agent for our test cases, but you are right, the way I solved this problem is not optimal. I'll commit improved version soon.

@ArturG

@PragTob done :)

@coveralls

Coverage Status

Coverage decreased (-14.37%) when pulling 2cebc66 on ArturG:coverage into 211605c on hacketyhack:master.

@PragTob
Collaborator

Hey there,

thanks! I believe that we do not need that user agent modification for all the specs/features. Although you are right that technically it would be the better refactoring/keeping the old behavior.

I believe it's only needed for checking that the right download is there so we could implement it as a background there so it is only set for that feature scenario and not for all of them :)

If I get to it before you I'll do it (should be ina couple of hours or tomorrow/this weekend) - thanks!

Tobi

@ArturG

You're right and I understand your point. Unfortunately, we cannot ignore this user-agent problem for other scenarios, because then we will get:

Failing Scenarios:
cucumber features/answers.feature:5 # Scenario: Create an answer
cucumber features/answers.feature:13 # Scenario: Edit an answer
cucumber features/blog.feature:17 # Scenario: Post to the blog
cucumber features/moderator.feature:8 # Scenario: Delete a question
cucumber features/moderator.feature:12 # Scenario: Normal users cannot moderate
cucumber features/programs.feature:9 # Scenario: View my programs
cucumber features/questions.feature:5 # Scenario: Create a question
cucumber features/questions.feature:11 # Scenario: Update a question
cucumber features/signup.feature:5 # Scenario: Create an account via the signup form
cucumber features/users.feature:8 # Scenario: View my profile

Because in case, when we do not define user-agent we get the following error (every time when we visit root page):

undefined method `downcase' for nil:NilClass (NoMethodError) 
./app/controllers/static_controller.rb:37:in `platform'
./app/controllers/static_controller.rb:3:in `root'

So there are two ways to solve the problem:

1st, define user-agent for cucumber

features/support/env.rb

Before do
  DatabaseCleaner[:mongo_mapper].clean
  page.driver.header('User-Agent', "Mac")
end

2nd, handle nil user-agent in StaticController

def platform
  request.user_agent.downcase
  request.user_agent.nil? ? nil : request.user_agent.downcase
end

P.S If I'm wrong, please, explain why :)

@coveralls

Coverage Status

Coverage decreased (-14.37%) when pulling 114bc62 on ArturG:coverage into 211605c on hacketyhack:master.

@PragTob
Collaborator

You are totally right! My bad! Needed that explanation, sorry :)

Just seems odd. I think a better solution would be to adjust the line of code. I think the 2nd solution sounds more robust, we don't want to fail if for some reason the UA is missing :)

@PragTob
Collaborator

Ah you already did that, awesome I like it!

Btw. great fast feedback loop on this PR :)

@PragTob PragTob merged commit b4f74ac into hacketyhack:master
@ArturG ArturG deleted the ArturG:coverage branch
@ArturG

Yeah, seems that the time zones difference is not a problem for us :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 27, 2014
  1. @ArturG

    Increase tests coverage

    ArturG authored
  2. @ArturG

    User-Agent refactoring

    ArturG authored
  3. @ArturG
This page is out of date. Refresh to see the latest.
View
1  Gemfile
@@ -42,6 +42,7 @@ group :development do
end
group :development, :test do
+ gem 'debugger'
gem 'fabrication'
gem 'rspec-rails'
gem 'capybara'
View
8 Gemfile.lock
@@ -64,6 +64,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.7.0)
+ columnize (0.3.6)
coveralls (0.7.0)
multi_json (~> 1.3)
rest-client
@@ -82,6 +83,12 @@ GEM
nokogiri (>= 1.5.0)
rails (>= 3.0.0)
database_cleaner (1.2.0)
+ debugger (1.6.5)
+ columnize (>= 0.3.1)
+ debugger-linecache (~> 1.2.0)
+ debugger-ruby_core_source (~> 1.3.1)
+ debugger-linecache (1.2.0)
+ debugger-ruby_core_source (1.3.2)
devise (2.2.8)
bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.1)
@@ -249,6 +256,7 @@ DEPENDENCIES
coveralls
cucumber-rails
database_cleaner
+ debugger
devise (~> 2.2.2)
fabrication
faker
View
2  app/controllers/answers_controller.rb
@@ -19,7 +19,5 @@ def update
format.html { redirect_to question_url(resource.question) }
end
end
-
-
end
View
11 app/controllers/static_controller.rb
@@ -23,7 +23,10 @@ def download
@other_platforms = ["mac", "windows", "linux"] - [@platform]
end
- def api_root; render :layout => "api"; end
+ def api_root
+ render :layout => "api"
+ end
+
def newest_version
render :json => {:version => "1.0.0"}
end
@@ -31,10 +34,6 @@ def newest_version
protected
def platform
- if Rails.env.test?
- "mac"
- else
- request.user_agent.match(/Mac|Linux|Windows/).try(:[], 0).try(:downcase)
- end
+ request.user_agent.nil? ? nil : request.user_agent.downcase
end
end
View
6 features/answers.feature
@@ -1,9 +1,9 @@
Feature: CRUD actions for answers
As a user of the site, I can perform standard CRUD actions on answers
-
+
Scenario: Create an answer
- Given a question exists
+ And a question exists
And I answer that question
Then I should be notified that my answer was submitted
And an email should be sent to the author
@@ -11,7 +11,7 @@ Feature: CRUD actions for answers
And my answer should show on my profile page
Scenario: Edit an answer
- Given a question exists
+ And a question exists
And I answer that question
When I edit that answer
Then I should see the updated answer
View
13 features/lessons.feature
@@ -0,0 +1,13 @@
+Feature: Hackety Lessons
+
+ As a user of the site, I can visit lessons page
+
+ Scenario: View the lessons
+ When I visit lessons path
+ Then I should see the list of lessons
+
+ Scenario: View specific lesson
+ When I visit lessons path
+ When I click on lesson's title
+ Then I should see lesson content
+
View
2  features/programs.feature
@@ -1,7 +1,7 @@
Feature: Access Programs
As a user of the site, I want to access users' programs
-
+
Scenario: View a featured program
Given there is a featured program
Then I should be able to view a highlighted program
View
2  features/signup.feature
@@ -1,7 +1,7 @@
Feature: Sign up for an account
As a new Hackety user, I want to create a new account and log in with it
-
+
Scenario: Create an account via the signup form
When I register a new account
Then I should be logged in with my new account
View
19 features/statics.feature
@@ -0,0 +1,19 @@
+Feature: Detect user's platform and provide download link
+
+ As a fresh Hackety user, I want to download software for my current OS
+
+ Scenario: MacOS User
+ Given "Mac" user agent
+ Then I should be able to download "mac" software
+
+ Scenario: Windows User
+ Given "Windows" user agent
+ Then I should be able to download "windows" software
+
+ Scenario: Linux User
+ Given "Linux" user agent
+ Then I should be able to download "linux" software
+
+ Scenario: Android User
+ Given "android" user agent
+ Then I should be able to download "android" software
View
17 features/step_definitions/lesson_steps.rb
@@ -0,0 +1,17 @@
+When(/^I visit lessons path$/) do
+ visit lessons_path
+end
+
+Then(/^I should see the list of lessons$/) do
+ page.should have_content("Lessons")
+ page.should have_content("A Tour of Hackety Hack")
+end
+
+When(/^I click on lesson's title$/) do
+ click_link("A Tour of Hackety Hack")
+end
+
+Then(/^I should see lesson content$/) do
+ expect(page).to have_title "A Tour of Hackety Hack"
+ page.should have_content("Welcome to the Hackety Hack tour!")
+end
View
8 features/step_definitions/static_steps.rb
@@ -0,0 +1,8 @@
+Given(/^"(.*?)" user agent$/) do |agent|
+ page.driver.header('User-Agent', agent)
+end
+
+Then(/^I should be able to download "(.*?)" software$/) do |system|
+ visit download_path
+ page.should have_content("Version 1.0.1 for #{system}")
+end
View
1  features/support/env.rb
@@ -8,6 +8,7 @@
require_relative '../../spec/code_coverage'
require 'cucumber/rails'
+require 'ruby-debug'
# Capybara defaults to XPath selectors rather than Webrat's default of CSS3. In
# order to ease the transition to Capybara we set the default here. If you'd
View
2  features/users.feature
@@ -2,7 +2,7 @@ Feature: Manage account
As a user of this site, I can view and update my profile, see my followers
- Background: The user is logged in
+ Background:
Given a logged in user
Scenario: View my profile
Something went wrong with that request. Please try again.