Rails测试失败

时间:2012-03-15 22:13:13

标签: ruby-on-rails testing

如果我像这样写测试它的工作就好了,它会传递绿色

it "should have the right title" do
        get 'view'
        response.should have_selector('title',
                                     :content => "View Snippets")
    end

但是对于同一页面,这个格式是红色的,他们会对同样的事情进行测试吗?

http://dl.dropbox.com/u/15024055/CloudShot/shot_15032012_232654.png

require 'spec_helper'

describe "SnippetsPages" do

    describe "New Snippet pages" do
        before { get 'new'}

        it { should have_selector('h1', content: 'New') }
        it { should have_selector('title', content: full_title('New Snippet')) }
    end


    describe "View Snippets pages" do
        before { get 'view' }

        it { should have_selector('h1', content: 'View') }
        it { should have_selector('title', content: full_title('View Snippets')) }
    end
end

我无法理解为什么我的测试不起作用这是帮手:

def full_title(page_title)
    base_title = "Sample App"
    if page_title.empty?
        base_title
    else
        page_title
    end
end

的ErrorMessage

Failures:

  1) SnippetsPages New Snippet pages
     Failure/Error: it { should have_selector('h1', content: 'New') }
       expected following output to contain a <h1>New</h1> tag:
       <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
       <html><body><p>New Snippet pages</p></body></html>
     # ./spec/requests/snippets_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

  2) SnippetsPages New Snippet pages
     Failure/Error: it { should have_selector('title', content: full_title('New Snippet')) }
       expected following output to contain a <title>Ruby on Rails Tutorial Sample App | New Snippet</title> tag:
       <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
       <html><body><p>New Snippet pages</p></body></html>
     # ./spec/requests/snippets_pages_spec.rb:9:in `block (3 levels) in <top (required)>'

  3) SnippetsPages View Snippets pages
     Failure/Error: it { should have_selector('h1', content: 'View') }
       expected following output to contain a <h1>View</h1> tag:
       <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
       <html><body><p>View Snippets pages</p></body></html>
     # ./spec/requests/snippets_pages_spec.rb:16:in `block (3 levels) in <top (required)>'

  4) SnippetsPages View Snippets pages
     Failure/Error: it { should have_selector('title', content: full_title('View Snippets')) }
       expected following output to contain a <title>Ruby on Rails Tutorial Sample App | View Snippets</title> tag:
       <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
       <html><body><p>View Snippets pages</p></body></html>
     # ./spec/requests/snippets_pages_spec.rb:17:in `block (3 levels) in <top (required)>'

Finished in 1.12 seconds
11 examples, 4 failures

Failed examples:

rspec ./spec/requests/snippets_pages_spec.rb:8 # SnippetsPages New Snippet pages
rspec ./spec/requests/snippets_pages_spec.rb:9 # SnippetsPages New Snippet pages
rspec ./spec/requests/snippets_pages_spec.rb:16 # SnippetsPages View Snippets pages
rspec ./spec/requests/snippets_pages_spec.rb:17 # SnippetsPages View Snippets pages

更新

替换得到访问没有工作她是一些更多的信息。

- 版本 -

➜  sample_app git:(modeling-users) ✗ rails -v
Rails 3.1.3

➜  sample_app git:(modeling-users) ✗ ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux]

➜  sample_app git:(modeling-users) ✗ rspec -v
2.6.4

- 结束版本 -

gemfile

source 'http://rubygems.org'

gem 'rails', '3.1.3'
gem 'bootstrap-sass', '2.0.0'

# Bundle edge Rails instead:
# gem 'rails',     :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.1.5'
  gem 'coffee-rails', '~> 3.1.1'
  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

group :development do
    gem 'rspec-rails'
    gem 'spork'
    gem 'guard-rspec', '0.5.5'
    gem 'annotate', '~> 2.4.1.beta'
end

group :test do
    gem 'rspec-rails'
    gem 'spork'
    gem 'webrat'
    gem 'capybara', '1.1.2'
    gem 'rb-inotify', '0.8.8'
    gem 'libnotify', '0.5.9'
end

➜sample_appgit :(建模用户)✗宝石列表

*** LOCAL GEMS ***

actionmailer (3.1.3)
actionpack (3.1.3)
active_reload (0.6.1)
active_utils (1.0.3, 1.0.2)
activemerchant (1.20.1)
activemodel (3.1.3)
activerecord (3.1.3)
activeresource (3.1.3)
activesupport (3.1.3)
acts_as_list (0.1.4)
akami (1.0.0)
annotate (2.4.1.beta1)
ansi (1.4.2)
arel (3.0.0, 2.2.3, 2.2.1)
bcrypt-ruby (3.0.1)
bootstrap-sass (2.0.0)
braintree (2.13.4)
builder (3.0.0)
bundler (1.0.21)
cancan (1.6.7)
capybara (1.1.2)
childprocess (0.3.1)
cocaine (0.2.1)
coffee-rails (3.1.1)
coffee-script (2.2.0)
coffee-script-source (1.2.0)
commonjs (0.2.0)
deface (0.7.2)
devise (1.4.8)
diff-lcs (1.1.3)
erubis (2.7.0)
execjs (1.3.0)
ffaker (1.12.1)
ffi (1.0.11)
githug (0.1.0)
grit (2.4.1)
guard (1.0.1)
guard-rspec (0.5.5)
gyoku (0.4.4)
highline (1.6.8)
hike (1.2.1)
httpi (0.9.6, 0.9.5)
i18n (0.6.0)
jquery-rails (1.0.19)
json (1.6.5)
kaminari (0.13.0)
less (2.0.9)
less-rails (2.1.6)
libnotify (0.5.9)
libv8 (3.3.10.4 x86-linux)
mail (2.3.0)
meta_search (1.1.1)
mime-types (1.17.2)
money (3.7.1)
multi_json (1.1.0, 1.0.4)
my_string_exstenderz (0.0.1)
nested_set (1.6.8)
nokogiri (1.5.0)
nori (1.1.0, 1.0.2)
orm_adapter (0.0.6)
paperclip (2.5.0)
polyamorous (0.5.0)
polyglot (0.3.3)
rack (1.3.6)
rack-cache (1.1)
rack-mount (0.8.3)
rack-ssl (1.3.2)
rack-test (0.6.1)
rails (3.1.3)
railties (3.1.3)
rake (0.9.2.2)
rb-inotify (0.8.8)
rdoc (3.12)
rspec (2.6.0)
rspec-core (2.6.4)
rspec-expectations (2.6.0)
rspec-mocks (2.6.0)
rspec-rails (2.6.1)
rubygems-update (1.8.17)
rubyzip (0.9.6.1)
sass (3.1.15, 3.1.14, 3.1.12)
sass-rails (3.1.5)
savon (0.9.9, 0.9.7)
selenium-webdriver (2.20.0)
spork (1.0.0rc1)
spree (1.0.0)
spree_api (1.0.0)
spree_auth (1.0.0)
spree_cmd (1.0.0)
spree_core (1.0.0)
spree_dash (1.0.0)
spree_promo (1.0.0)
spree_sample (1.0.0)
sprockets (2.0.3)
sqlite3 (1.3.5)
state_machine (1.1.1)
stringex (1.3.2, 1.3.0)
therubyracer (0.9.10)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
turn (0.8.2)
twitter-bootstrap-rails (2.0.3)
tzinfo (0.3.31)
uglifier (1.2.3)
warden (1.0.6)
wasabi (2.1.0, 2.0.0)
webrat (0.7.3)
xpath (0.1.4)

1 个答案:

答案 0 :(得分:0)

itsubject上调用匹配器。

我认为,subject在第二种情况下不是response

如果您使用的是水豚,只需将get替换为visit,一切都会好的。 如果我在规范中将visit替换为get,至少会收到类似的错误:)

如果这无济于事,请在您的主题中添加有关环境的更多信息。如红宝石和铁轨版,二手宝石等。