Rspec2 Rails3 rake规范失败

时间:2011-03-09 08:01:52

标签: json ruby-on-rails-3 show rspec2


我可以通过运行RubyMine运行部分代码来运行我的测试(右键单击_spec.rb文件并运行)。但是,通过运行“rake spec”我得到一个错误。该表被删除,并且不会在每次测试时重建。我知道它是有目标的,但如果不再重建则不会 比如db:test:load,db:test:prepare,db:test:clone_sturcture也行不通。当我运行“rake spec”时我得到了这个错误:

C:\RubyStack\ruby\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) C:\RubyStack\ruby\bin\rake spec
Testing started at 14:11 ...
(in E:/idun/ComServer_v0.0/source)
C:/RubyStack/ruby/bin/ruby.exe -S bundle exec rspec ./spec/controllers/customer_controller_spec.rb
Empty test suite.

ActiveRecord::StatementInvalid: Mysql2::Error: Table 'comserver_test.customers' doesn't exist: DELETE FROM `customers`
C:/RubyStack/ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract_adapter.rb:207:in `rescue in log'
C:/RubyStack/ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract_adapter.rb:199:in `log'
C:/RubyStack/ruby/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6-x86-mingw32/lib/active_record/connection_adapters/mysql2_adapter.rb:314:in `execute'
C:/RubyStack/ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/database_statements.rb:288:in `update_sql'
C:/RubyStack/ruby/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6-x86-mingw32/lib/active_record/connection_adapters/mysql2_adapter.rb:331:in `update_sql'
C:/RubyStack/ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/database_statements.rb:293:in `delete_sql'
C:/RubyStack/ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/database_statements.rb:54:in `delete'
C:/RubyStack/ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/query_cache.rb:16:in `delete'
C:/RubyStack/ruby/lib/ruby/gems/1.9.1/gems/arel-2.0.9/lib/arel/crud.rb:34:in `delete'
C:/RubyStack/ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.5/lib/active_record/relation.rb:273:in `delete_all'
C:in `delete_all'
E:/idun/ComServer_v0.0/source/spec/controllers/customer_controller_spec.rb:20:in `block (2 levels) in <top (required)>'
C:/RubyStack/ruby/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:29:in `instance_eval'

* _ spec.rb:

require 'spec_helper'
require 'customer'
require 'rack/test'
require 'rspec'
require 'logger'

RSpec.configure do |conf|
  conf.include Rack::Test::Methods
end

def app
  Rails::Application
end

describe "customers" do

  render_views

  before(:each) do
    Customer.delete_all
  end

  describe "GET on /customers/:id" do
    before(:each) do
      Customer.create(
        :gmsid => 1,
        :online => true,
        :pinged_at => DateTime.now
      )
    end

    it "should return a customer by id" do
      @response = get("/customers/1")
      @response.status.should eq(200)
      attributes = ActiveSupport::JSON.decode(@response.body)
      attributes['customer']["online"].should be_true
    end

    it "should return \"null\" if customer not found" do
      @response = get('/customers/2')
      @response.body.to_s.should eq("null")
    end
  end

  describe "should POST on /customers" do
    it "should create a user" do
      customer =  Customer.new(
        :gmsid => 2,
        :online => false,
        :pinged_at => DateTime.now
      )
      puts customer
      json = ActiveSupport::JSON.encode(customer)
      @response = post('/customers', json)
      @response.status.should eq(200)
      @response = get('/customers/2')
      attributes = ActiveSupport::JSON.decode(@response.body)
      attributes['customer']['gmsid'].should eq(2)
      attributes['customer']['online'].should be_false
    end
  end

  describe "should PUT on /customers/:id" do
     before(:each) do
      Customer.create(
        :gmsid => 1,
        :online => false,
        :pinged_at => DateTime.now
      )
    end

    it "should update a user to status true" do
      @response = put('/customers/1', ActiveSupport::JSON.encode(Customer.new(
          :online => true
      )))
      @response.status.should eq(200)
      @response = get('/customers/1')
      attributes = ActiveSupport::JSON.decode(@response.body)
      attributes['customer']['online'].should be_true

    end

    it "when user doesn't exist should return null" do
      @response = put('/customers/2', ActiveSupport::JSON.encode(Customer.new(
          :online => true
      )))
      @response.status.should eq(200)
      @response.body.to_s.should eq("null")
    end
  end

  describe "should DELETE on /customers/:id" do
    it "should delete a existing user" do
      Customer.create(
          :gmsid => 1,
          :online => true,
          :pinged_at => DateTime.now
      )
      @response = get('/customers/1')
      #test if customer with gmsid 1 is in database
      @response.status.should eq(200)
      attributes = ActiveSupport::JSON.decode(@response.body)
      attributes['customer']['gmsid'].should eq(1)
      #delete customer with gmsid 1
      @response = delete ('/customers/1')
      @response.status.should eq(200)
      #check if customer with gmsid 1 is not in database anymore
      @response = get ('/customers/1')
      @response.status.should eq(200)
      @response.body.to_s.should eq("null")
    end
  end
end

1 个答案:

答案 0 :(得分:0)

好的,我解决了这个问题。

讨论了here的“libmysql.dll问题”。

问题是mysql 5.0版本。它应该高于5.1。所以,我刚刚将“libmysql.dll”从一个较新的mysql版本改为一个。有效。 您需要从开发环境创建适当的转储(db:schema:dump) 此命令在项目中创建schema.rb 这个模式由“db:test:prepare”使用,每次执行“rake spec”时都会调用它,然后再进行测试。此schema.rb为空,因此未创建任何表。 对我来说听起来很合理。

如果我错了,请纠正我。