小编kha*_*hal的帖子

Rspec:如何测试ActiveRecord :: Base.connection.execute

我正在使用原始/裸机sql插件来提高我的服务的写入性能.我的模块中有这样的东西 -

insert = "('#{id}', '#{status}', '#{some_time_val}')"
sql_string = "INSERT INTO history ('device_id', 'status', 'time') VALUES #{insert}"
ActiveRecord::Base.connection.execute sql_string
Run Code Online (Sandbox Code Playgroud)

当我写下面的rspec时,除了插入是否通过之外,它还会测试所有内容.所以我的期望永远不会有效,因为rspec,database_cleaner等做回滚和交易的方式.我试过用

  self.use_transactional_fixtures = false
Run Code Online (Sandbox Code Playgroud)

  before(:all) do
    DatabaseCleaner.strategy = nil
  end
Run Code Online (Sandbox Code Playgroud)

但插入仍然没有进入我的测试数据库

describe Worker do
  let (:device1) {FactoryGirl.create(:device)}
  let (:device2) {FactoryGirl.create(:device)}
  let (:device3) {FactoryGirl.create(:device)}
  self.use_transactional_fixtures = false

  before(:all) do
    DatabaseCleaner.strategy = nil
  end

  it "does something" do
    devices = [{"status" => "Offline", "time" => "2013-09-17 18:17:17", "id" => device1.id},
                {"status" => "Online", "time" => "2013-09-17 18:18:18", "id" => device2.id}]
    Worker.any_instance.stubs(:devices).returns(devices) ## …
Run Code Online (Sandbox Code Playgroud)

ruby activerecord rspec ruby-on-rails

9
推荐指数
1
解决办法
3051
查看次数

在带有 typhoeus 的 Rails 应用程序中将哈希数组作为参数传递给 POST 的问题

我有两个导轨服务。一个服务于 UI 和一些基本功能 (UIService),另一个管理底层模型和数据库交互 (MainService)。

在 UIService 中,我有一个表单,它收集项目列表并使用它通过 jQuery POST 到 MainService。

我首先使用 javascript 数组并调用 jQuery.post 到 UIService,就像这样 -

var selected_items = new Array();
// Filled up via the form...
params={"name":$("#name_input").val(),
        "items": selected_items };
jQuery.post("/items", params);
Run Code Online (Sandbox Code Playgroud)

然后将其转换为带有键“item_id”的散列数组,然后通过 Typhoeus 像这样转发到 MainService -

items = []
item = {}
params[:items].each do |i|
  item[:item_id] = i
end
## Gives me this ---> items = [ {item_id: 189}, {item_id: 187} ]

req = Typhoeus::Request.new("#{my_url}/items/", 
                            method: :POST, 
                            headers: {"Accepts" => "application/json"})
hydra = Typhoeus::Hydra.new
hydra.queue(req) …
Run Code Online (Sandbox Code Playgroud)

arrays hash json ruby-on-rails typhoeus

3
推荐指数
1
解决办法
2773
查看次数

标签 统计

ruby-on-rails ×2

activerecord ×1

arrays ×1

hash ×1

json ×1

rspec ×1

ruby ×1

typhoeus ×1