我正在尝试编写一个模拟来自Dropbox的REST服务的一些返回值的测试,该服务通过嵌套哈希为我提供数据.
我无法弄清楚如何编码我的工厂,因为返回结果是一个带有内部的数组.会怎么样?
Factory.define :dropbox_hash do
??
end
Run Code Online (Sandbox Code Playgroud)
Dropbox数据如下所示:
["/home", {"revision"=>48, "rev"=>"30054214dc", "thumb_exists"=>false, "bytes"=>0, "modified"=>"Thu, 29 Dec 2011 01:53:26 +0000", "path"=>"/Home", "is_dir"=>true, "icon"=>"folder_app", "root"=>"app_folder", "size"=>"0 bytes"}]
Run Code Online (Sandbox Code Playgroud)
在我的RSpec中,我想要像这样的工厂电话:
Factory.create(:dropbox_hash)
Run Code Online (Sandbox Code Playgroud) 将Rails应用程序升级到Rails 5后,运行RSpec测试会出现以下错误:
rails aborted!
ActiveRecord::NoEnvironmentInSchemaError:
Environment data not found in the schema. To resolve this issue, run:
bin/rails db:environment:set RAILS_ENV=test
Run Code Online (Sandbox Code Playgroud)
但是,这bin不存在,我似乎无法使用bundle binstubs rails或生成它rake rails:update:bin.
我也尝试过:
rails db:environment:set RAILS_ENV=test
rake db:environment:set RAILS_ENV=test
Run Code Online (Sandbox Code Playgroud)
有在Github上一个相关的问题在这里.
我该如何解决这个错误?
在我开始组合我的Redux减速器之前,我的应用程序工作正常.但是当我合并时,initialState和reducer键会混淆.
function flash(state = [], action) {
switch (action.type) {
case FLASH_MESSAGE_UPDATED:
return _.extend({}, state, { flash: action.flash })
default:
return state
}
}
function events(state = [], action) {
switch (action.type) {
case EVENTS_UPDATED:
return _.extend({}, state, { events: action.pathway_events })
default:
return state
}
}
export default combineReducers({
events,
flash
})
Run Code Online (Sandbox Code Playgroud)
这会导致功能损坏和控制台错误:
Unexpected keys "one", "two" found in initialState argument passed to createStore. Expected to find one of the known reducer keys instead: "events", "flash". Unexpected …
我正在尝试在Rails中编写Rspec测试,使用Devise帮助方法进行登录和注销.sign_in方法无效.但是,在对应用程序进行一系列更改之前,它已经提前工作了.
我尝试过的事情:
到目前为止,没有骰子.使用已登录用户测试控制器需要做些什么?
错误信息:
OrderItemsController GET #index renders the :index view
Failure/Error: sign_in :admin
NoMethodError:
undefined method `sign_in' for # <RSpec::ExampleGroups::OrderItemsController_2::GETIndex:0x00000102c002d0>
# ./spec/controllers/order_items_controller_spec.rb:6:in `block (2 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)
控制器规格
require 'spec_helper'
describe OrderItemsController do
before (:each) do
admin = create(:admin)
sign_in :admin
end
describe "GET #index" do
it "renders the :index view" do
get :index
expect( response ).to render_template :index
end
end
end
Run Code Online (Sandbox Code Playgroud)
spec_helper.rb
require 'rspec/rails'
require 'capybara/rspec'
RSpec.configure do |config|
config.include ApplicationHelper
config.include …Run Code Online (Sandbox Code Playgroud) 我有一个rake任务,根据环境防范危险的Rails rake rasks.它工作正常.当我在RSpec中测试每个单独的危险方法时,测试通过.当我连续测试多个时,对于多个环境,测试在第一个测试后失败.例如,即使我为同一危险动作多次运行测试rake db:setup,它也只会在第一次通过.如果我将测试作为单独的it语句运行,每个危险操作一个,只有前两个将通过(有4个).
如何在这里使RSpec正常运行,并在套件中运行时通过所有测试?
耙子任务
# guard_dangerous_tasks.rake
class InvalidTaskError < StandardError; end
task :guard_dangerous_tasks => :environment do
unless Rails.env == 'development'
raise InvalidTaskError
end
end
%w[ db:setup db:reset ].each do |task|
Rake::Task[task].enhance ['guard_dangerous_tasks']
end
Run Code Online (Sandbox Code Playgroud)
RSpec测试
require 'spec_helper'
require 'rake'
load 'Rakefile'
describe 'dangerous_tasks' do
context 'given a production environment' do
it 'prevents dangerous tasks' do
allow(Rails).to receive(:env).and_return('production')
%w[ db:setup db:reset ].each do |task_name|
expect { Rake::Task[task_name].invoke }.to raise_error(InvalidTaskError)
end
end
end
context 'given a test …Run Code Online (Sandbox Code Playgroud) 我正在尝试从elm-lang教程修改一个简单的应用程序,首先更新模型,然后触发另一个更新.
update msg model =
case msg of
MorePlease ->
(model, getRandomGif model.topic)
NewGif (Ok newUrl) ->
( { model | gifUrl = newUrl }, Cmd.none)
NewGif (Err _) ->
(model, Cmd.none)
-- my addition
NewTopic newTopic ->
({ model | topic = newTopic}, MorePlease)
Run Code Online (Sandbox Code Playgroud)
这在编译器中失败,因为NewTopic分支:
The 3rd branch has this type:
( { gifUrl : String, topic : String }, Cmd Msg )
But the 4th is:
( { gifUrl : String, topic : String }, Msg …Run Code Online (Sandbox Code Playgroud) 我想知道在Rails中解析文本查询的最佳方法是什么,允许用户包含逻辑运算符?
我希望用户能够输入其中任何一个或等效的:
# searching partial text in emails, just for example
# query A
"jon AND gmail" #=> ["jonsmith@gmail.com"]
# query B
"jon OR gmail" #=> ["jonsmith@gmail.com", "sarahcalaway@gmail.com"]
# query C
"jon AND gmail AND smith" #=> ["jonsmith@gmail.com"]
Run Code Online (Sandbox Code Playgroud)
理想情况下,我们可以用括号来表示更复杂的操作顺序,但这不是必需的.
是否有宝石或图案支持这个?
在架构格式的情况下忽略表是很简单的:ruby,但是当架构格式是有办法的时候:sql吗?
理想情况是这样的environment.rb:
ActiveRecord::SQLDumper.ignore_tables = ['table_name']
Run Code Online (Sandbox Code Playgroud)
通过AR源代码快速阅读后,它看起来没有希望.
我希望能够knex在命令行上回滚特定的迁移。
例如:
knex migrate:rollback('20161104101325')
Run Code Online (Sandbox Code Playgroud)
这可能吗?
我在Rails 4中定制Spree 2.3应用程序.当我保存price.amount时,它似乎在数据库中正确保存,但是当我检索它时,它被包装在BigDecimal类中并返回0.
如何存储或检索正确的值?
# in the form
<%= number_field_tag :amount %>
# controller action
@variant.price = params[:amount]
# appears in PostgresQL database as type 'numeric'
id: 35, amount: 60.00
# retrieved in Rails console as BigDecimal class instance
# looks like the wrong amount
2.1.1 :001 > Spree::Price.find_by_id(35).amount
=> #<BigDecimal:105806d20,'0.0',9(27)>
# converts to 0.0
2.1.1 :002 > Spree::Price.find_by_id(35).amount.to_f
=> 0.0
# testing data retrieval in the console
2.1.1 :003 > ActiveRecord::Base.connection.execute("SELECT * FROM spree_prices WHERE id=35").first
=> {"id"=>"35", "variant_id"=>"35", …Run Code Online (Sandbox Code Playgroud) rspec ×4
activerecord ×2
devise ×1
elm ×1
express ×1
factory-bot ×1
knex.js ×1
rake ×1
reactjs ×1
redux ×1
redux-thunk ×1
rspec-rails ×1
rspec3 ×1
ruby ×1
ruby-2.0 ×1
spree ×1