Last active
December 11, 2015 09:28
-
-
Save keating/4579677 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#1 | |
# 测试登录,omniauth-identity | |
it "will show save search button in home" do | |
OmniAuth.config.test_mode = false | |
user ||= FactoryGirl.create(:identity_first) | |
visit login_path | |
within("form") do | |
fill_in 'auth_key', with: user.email | |
fill_in 'password', with: user.password | |
click_button "LogIn" | |
end | |
page.current_path.should eq root_path | |
page.should have_button "Save Search" | |
end | |
#2 | |
#常写的controller测试 | |
describe PagesController do | |
context "Anonymous user" do | |
[:about, :advertise, :contact, :disclaimer, | |
:help, :home, :service, | |
].each do |action| | |
describe "GET #{action}" do | |
before(:each) { get action } | |
it "renders the #{action} page" do | |
response.should be_success | |
response.should render_template action | |
end | |
end | |
end | |
end | |
end | |
#3 | |
#下面用来测试页面中是否存在字母,在非英文环境下偶尔会需要 | |
context "anonymous non-English users, untranslated text in" do | |
before(:all) { stub_translate } | |
after( :all) { unstub_translate } | |
[:about, :help].each do |action| | |
describe "GET #{action}'s" do | |
before(:each) do | |
get action | |
exceptions = [] | |
s = strip_from_response exceptions | |
@doc = expect_no_letters_doc s | |
end | |
it "title" do | |
expect_no_letters @doc.title | |
end | |
it "body" do | |
expect_no_letters @doc | |
end | |
end | |
end | |
end | |
#需要 include下面的 module | |
module ExpectNoLetters | |
def expect_no_letters(v) | |
(v.kind_of? Nokogiri::HTML::Document) ? | |
(expect_no_letters_in_doc v) : | |
(expect_no_letters_in_string_array [*v]) | |
nil | |
end | |
def expect_no_letters_doc(s) Nokogiri::HTML s end | |
# Exceptions is an array of fixed strings and regexes | |
def strip_from_response(exceptions) | |
s = response.body | |
[*exceptions].each{|e| s = s.gsub e, ''} | |
s | |
end | |
def stub_translate() $translate_stubbed = true ; nil end | |
def unstub_translate() $translate_stubbed = false; nil end | |
def expect_no_letters_in_doc(doc) | |
exceptions = %w[ pre script style ] | |
nodes = doc.css 'body, body *' | |
nodes.each{|e| e.content='' if exceptions.include? e.name} | |
expect_no_letters_in_string_array nodes.map &:content | |
nil | |
end | |
def expect_no_letters_in_string_array(a) | |
big = a.map{|e| e.gsub /\s/, ''}.join ' ' | |
letters_regex = /[a-zA-Z]/ | |
show_length = 30 | |
m = big =~ letters_regex | |
s = m.nil? ? '' : "Untranslated text starts: `#{big.slice m, show_length}'" | |
m.nil?.should be(true), s | |
nil | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment