首页 \ 问答 \ Rails为测试环境加载不正确的数据库配置?(Rails loading incorrect database config for test env?)

Rails为测试环境加载不正确的数据库配置?(Rails loading incorrect database config for test env?)

有一个奇怪的问题...我的测试继续对我的开发数据库运行。 我在这里尝试了test_helper.rb中的test_helper.rb

# this is the top of the file
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
binding.pry

并注意到3件事情:

  1. ENV['RAILS_ENV'] / Rails.env被正确设置为"test"
  2. Rails配置似乎加载了正确的值:

    Rails.application.config.database_configuration[Rails.env]
    
    # => {"adapter"=>"postgis",
    #     "encoding"=>"unicode",
    #     "pool"=>5,
    #     "username"=>"user",
    #     "host"=>"localhost",
    #     "database"=>"db_test"} <= CORRECT
    
  3. ...但ActiveRecord加载一个不正确的值:

    ActiveRecord::Base.configurations[Rails.env]
    
    # => {"adapter"=>"postgis",
    #     "encoding"=>"unicode",
    #     "pool"=>5,
    #     "username"=>"user",
    #     "host"=>"localhost",
    #     "port"=>5432,
    #     "database"=>"db_development"} <= INCORRECT
    

另外, ActiveRecord::Base.configurations[Rails.env]返回一个port密钥,而Rails.application.config.database_configuration[Rails.env]不会。


为什么这些不同? 这是我的配置:

default: &default
  adapter: postgis
  encoding: unicode
  pool: 5
  username: user
  host: localhost

development:
  <<: *default
  database: db_development

test:
  <<: *default
  database: db_test

grep ed我的整个项目文件夹和config/database.yml是我命名我的数据库的唯一地方,所以没有其他配置覆盖这个据我所知。

我正在运行rails 4.2.5

帮帮我!


Have a weird issue... my tests keep running against my development database. I tried binding.pry in test_helper.rb here:

# this is the top of the file
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
binding.pry

And noticed 3 things:

  1. ENV['RAILS_ENV']/Rails.env was set correctly to "test".
  2. Rails configuration seems to load the correct value:

    Rails.application.config.database_configuration[Rails.env]
    
    # => {"adapter"=>"postgis",
    #     "encoding"=>"unicode",
    #     "pool"=>5,
    #     "username"=>"user",
    #     "host"=>"localhost",
    #     "database"=>"db_test"} <= CORRECT
    
  3. ... but ActiveRecord loads an incorrect value:

    ActiveRecord::Base.configurations[Rails.env]
    
    # => {"adapter"=>"postgis",
    #     "encoding"=>"unicode",
    #     "pool"=>5,
    #     "username"=>"user",
    #     "host"=>"localhost",
    #     "port"=>5432,
    #     "database"=>"db_development"} <= INCORRECT
    

Additionally, ActiveRecord::Base.configurations[Rails.env] returns a port key whereas Rails.application.config.database_configuration[Rails.env] does not.


Why are these different? Here's my config:

default: &default
  adapter: postgis
  encoding: unicode
  pool: 5
  username: user
  host: localhost

development:
  <<: *default
  database: db_development

test:
  <<: *default
  database: db_test

I greped my entire project folder and config/database.yml is the only place I name my databases, so there's not another config overriding this one as far as I know.

I'm running rails 4.2.5.

Help!


原文:https://stackoverflow.com/questions/37469561
更新时间:2023-10-19 08:10

最满意答案

我设法让它工作,我90%确定可能是潜在的问题。 我原本以为设置主机视图,图形和绘图的顺序可能是问题,但结果却并非如此。

修订后的代码是:

protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
    base.OnElementChanged(e);
    var view = NativeView;

    var graph = new CPTXYGraph
    {
        Title = "Pie Chart",

    };

    var hostView = new CPTGraphHostingView(view.Bounds);
    view.AddSubview(hostView);

    var plot = new CPTPieChart
    {
        DataSource = new PiePlotDataSource(),
        PieRadius = hostView.Bounds.Size.Height / 4,
    };

    graph.AddPlot(plot);   

    hostView.HostedGraph = graph;            
}

我更改的是,在创建CPTGraphHostingView() ,我将父View.BoundsView.Bounds作为参数传递,然后将结果分配给变量hostView ,稍后将使用该变量。 我相信这是造成布局问题的原因。

然后在设置饼图的PieRadius属性时使用PieRadius 。 如果未设置此属性,则不会显示饼图(仅显示一组X和Y轴)。

就未调用的PiePlotDataSource方法而言,简单的清理和重建修复了该问题。 其NumberForPlot()方法的更新代码如下:

public override NSNumber NumberForPlot(CPTPlot plot, CPTPlotField forFieldEnum, uint index)
{
    return forFieldEnum == CPTPlotField.BarLocation ? DummyData.Data[index] : index;
}

请注意,我将forFieldEnumCPTPlotField.BarLocation进行比较,而不是CPTPlotField.PieChartWidth 。 这是因为我提出了一些断点并逐步完成了该方法,结果发现forFieldEnum始终是CPTPlotField.BarLocation 。 我相信这是Xamarin CorePlot Compoenent的某种绑定错误。


I have managed to get it working and I am 90% sure what may have been the underlying problem. I originally thought that the order of setting up the host view, graph and plot may have been the problem but it turns out it was not the case.

The revised code is:

protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
    base.OnElementChanged(e);
    var view = NativeView;

    var graph = new CPTXYGraph
    {
        Title = "Pie Chart",

    };

    var hostView = new CPTGraphHostingView(view.Bounds);
    view.AddSubview(hostView);

    var plot = new CPTPieChart
    {
        DataSource = new PiePlotDataSource(),
        PieRadius = hostView.Bounds.Size.Height / 4,
    };

    graph.AddPlot(plot);   

    hostView.HostedGraph = graph;            
}

What I have changed is that when creating a CPTGraphHostingView(), I pass in the parent's View.Bounds as a parameter and then assign the result to the variable hostView, which will be used later. I believe this was what was causing the layout problems.

The hostView is then used when setting the PieRadius property of the pie chart. If this property isn't set, the pie chart does not show (only a set of X and Y axes are shown).

In terms of the PiePlotDataSource methods not being called, a simple clean and rebuild fixed that problem. The updated code for its NumberForPlot() method is here:

public override NSNumber NumberForPlot(CPTPlot plot, CPTPlotField forFieldEnum, uint index)
{
    return forFieldEnum == CPTPlotField.BarLocation ? DummyData.Data[index] : index;
}

Notice that I am comparing forFieldEnum to CPTPlotField.BarLocation instead of CPTPlotField.PieChartWidth. This was because I put some break points in and stepped through the method, only to find that forFieldEnum was always CPTPlotField.BarLocation. I believe this is a binding error of some sort to the Xamarin CorePlot Compoenent.

相关问答

更多