未定义的方法`它' for main:Object(NoMethodError)

时间:2014-11-25 05:35:41

标签: ruby rspec sinatra

我的代码出了什么问题?它说当我试图进行测试时我的“它”没有定义

  

shaunstanislaus @ Master~ / workspace / sinatra_practice / crud(master)$ rspec app_test.rb   /Users/shaunstanislaus/workspace/sinatra_practice/crud/app_test.rb:1:in <top (required)>': undefined method it'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: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:in load_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:in run'       来自/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:in调用'       来自/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:in load'       来自/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:in eval'       来自/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

1 个答案:

答案 0 :(得分:6)

您的测试应位于描述块

require 'spec_helper'

describe MyController do 

  it "creates a new bookmark" do
    ..
  end

  ..
end