【问题标题】:undefined method `it' for main:Object (NoMethodError)main:Object (NoMethodError) 的未定义方法“it”
【发布时间】:2015-01-23 00:42:21
【问题描述】:

我的代码有什么问题?它说我在尝试进行测试时没有定义我的“它”

shaunstanislaus@Master ~/workspace/sinatra_practice/crud (master) $ rspec app_test.rb /Users/shaunstanislaus/workspace/sinatra_practice/crud/app_test.rb:1:in &lt;top (required)&gt;': undefined methodit' for main:Object (NoMethodError) 来自 /Users/shaunstanislaus/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in load' from /Users/shaunstanislaus/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:inblock in load_spec_files' 来自 /Users/shaunstanislaus/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in each' from /Users/shaunstanislaus/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:inload_spec_files' 来自 /Users/shaunstanislaus/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:96:in setup' from /Users/shaunstanislaus/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:84:inrun' 来自 /Users/shaunstanislaus/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:69:in run' from /Users/shaunstanislaus/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:37:ininvoke' 来自 /Users/shaunstanislaus/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.1.7/exe/rspec:4:in <top (required)>' from /Users/shaunstanislaus/.rvm/gems/ruby-2.1.2/bin/rspec:23:inload' 来自 /Users/shaunstanislaus/.rvm/gems/ruby-2.1.2/bin/rspec:23:in <main>' from /Users/shaunstanislaus/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:ineval' 来自 /Users/shaunstanislaus/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `'

it "creates a new bookmark" do
  get "/bookmarks"
  bookmarks = JSON.parse
(last_response.body)
  last_size = bookmarks.size

  post "/bookmarks",
    {:url => "http://www.test.com", :title => "Test"}

  last_response.status.should == 201
  last_response.body.should match(/\/bookmarks\/\d+/)


  get "/bookmarks"
  bookmarks = JSON.parse
(last_response.body)
  expect(bookmarks.size).to eq(last_size + 1)

end

【问题讨论】:

    标签: ruby rspec sinatra


    【解决方案1】:

    您的测试应该在描述块内

    require 'spec_helper'
    
    describe MyController do 
    
      it "creates a new bookmark" do
        ..
      end
    
      ..
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-22
      • 2023-03-30
      • 1970-01-01
      相关资源
      最近更新 更多