简体   繁体   English

RAILS_ENV =在以下之前在rspec中运行的rake任务中被忽略的测试:全部

[英]RAILS_ENV=test being ignored in rake tasks running within rspec before:all

Rails 3.2.1, Ruby 1.9.3p125, rspec 2.8.0, rake 0.9.2.2 Rails 3.2.1,Ruby 1.9.3p125,rspec 2.8.0,rake 0.9.2.2

Rake::Task['namespace:task_name'].invoke refused to find namespace:task_name (namespace.rake is in Rails.root/lib/tasks). Rake :: Task ['namespace:task_name']。invoke拒绝找到命名空间:task_name(namespace.rake在Rails.root / lib / tasks中)。 .execute - same. .execute-相同。

So, I'm doing 所以,我在做

system 'RAILS_ENV=test bundle exec rake namespace:taskname'

which appears to work but -- does not use test database. 看起来可行,但是-不使用测试数据库。

I have a module and have been told to write tests for it within Rails framework. 我有一个模块,并被告知要在Rails框架中为其编写测试。 I'm using rspec. 我正在使用rspec。 I have the cookbook task that creates fixtures from my "sandbox" environment ("development" is somewhere else and inaccessible to me). 我有一个食谱任务,可以从我的“沙盒”环境中创建固定装置(“开发”在其他地方并且对我来说是无法访问的)。 The fixtures are created correctly. 灯具已正确创建。

Next, I try to run 接下来,我尝试运行

system 'RAILS_ENV=test bundle exec rake db.fixtures.load FIXTURES=<fixture_list>'

No data in my test db; 我的测试数据库中没有数据; I'm just assuming that it's being loaded back into my sandbox db, which is the same place from which it was derived. 我只是假设它已被加载回我的沙箱数据库中,这与它的来源相同。

The purpose of my module is to examine other tables in the database, and for each day, to create or not create a "feedstatus" record for each of 34 feeds. 我的模块的目的是检查数据库中的其他表,并每天为34个提要中的每个提要创建或不创建“ feedstatus”记录。 Whether the record gets created depends on predetermined schedules, whether the "feed" observes a holiday, what day of the week it is - many criteria that vary wildly but are algorithmically determined (many algorithms here). 记录是否被创建取决于预定的时间表,“提要”是否遵守假期,是星期几-许多标准差异很大,但都是通过算法确定的(此处有许多算法)。

I could write several hundred tests to test each possible outcome for each "feed" but (a) I think that's a lot of work; 我可以编写数百个测试来测试每个“供稿”的每种可能结果,但是(a)我认为这需要大量工作; (b) it would be unmaintainable (more "feeds" will be added in the future); (b)这将是无法维持的(将来会增加“饲料”); (c) I'm still working on heuristics about the algorithms - something new or something I didn't anticipate is always appearing and (d) I'm concerned that I will revert to using the same algorithms that the module uses, which tests nothing but my typing. (c)我仍在研究有关算法的启发式方法-总是出现新的东西或意料之外的东西,并且(d)我担心我会恢复使用模块使用的相同算法进行测试除了我的打字。

So, the plan was to create a spreadsheet matrix of all the days of the year x all the "feeds' (34x366) and manually put a 0 or a 1 to indicate the "feed" shouldn't or should be there that day. Yes, it's a bit of work, but the people who follow me will be more comfortable with changing spreadsheets than ruby code, and any discrepancies will show up. 因此,计划是创建一个包含一年中所有天数x所有“提要”(34x366)的电子表格矩阵,并手动将0或1表示“提要”在该天不应该存在。是的,这需要一些工作,但是跟随我的人比使用ruby代码更愿意更改电子表格,并且会出现任何差异。

So, the steps are: 1. prepare the empty test database 2. create the fixtures (every time to catch changes to the real data) 3. load the fixtures 4. run the module for a year's worth of days. 因此,步骤为:1.准备空的测试数据库2.创建夹具(每次都捕获对真实数据的更改)3.加载夹具4.将模块运行一年的时间。 5. For each spreadsheet entry, match it to presence or absence of the record that the module will or will not create. 5.对于每个电子表格条目,将其与模块将要创建或将不会创建的记录的存在或不存在相匹配。

But - I'm stuck at "load the fixtures". 但是-我被困在“加载固定装置”上。 No data gets loaded into the test db. 没有数据加载到测试数据库中。

Incidentally, invoking the module with 顺便说一句,使用

system 'RAILS_ENV=test bundle exec rake namespace:task_that_calls_module'

causes the module to use the sandbox (current environment) rather than the test db. 使模块使用沙箱(当前环境)而不是测试数据库。

The task_that_calls_module also has RAILS_ENV=test name_of_module_method_to_perform and still writes the sandbox, not the test db. task_that_calls_module也具有RAILS_ENV = test name_of_module_method_to_perform,并且仍然写入沙箱,而不是测试数据库。 "sandbox" is my actual development environment. “沙盒”是我实际的开发环境。

The rspec file in Rails.root/spec/modules: Rails.root / spec / modules中的rspec文件:

describe "MakeExpected" do
require 'rake'

  before(:all) do
    system "bundle exec rake testdb:extract_fixtures"
    puts "extracted fixtures"

    system "bundle exec rake db:test:load"
    puts "db:test:load"

    system 'RAILS_ENV=test bundle exec rake db:fixtures:load'
    puts "loaded fixtures"

    system("RAILS_ENV=test bundle exec rake testdb:run_make_expected --trace")
  end     # before

  it "should be true" do
  end     # should be true

end     # describe

The testdb:run_make_expected task in Rails.root/lib/tasks/testdb.rake: Rails.root / lib / tasks / testdb.rake中的testdb:run_make_expected任务:

  task :run_make_expected => :environment do
    include MakeExpected
    RAILS_ENV=test test_scheduler
  end     # task do

After the before block, there will be some convoluted tests comparing db with expectations from the spreadsheet. 在before块之后,将进行一些复杂的测试,将db与电子表格中的期望进行比较。

Please tell me it's something dumb and simple? 请告诉我这有点愚蠢而简单吗?

Oh - the bundle exec db rake stuff is because I don't have root to install rake into the regular bin directory. 哦-捆绑exec db rake的东西是因为我没有root可以将rake安装到常规bin目录中。 Same with rspec. 与rspec相同。 I invoke this whole thing as: 我将整个事情称为:

bundle exec rspec spec/modules

where this make_expected_spec.rb is the only file in that directory. 该make_expected_spec.rb是该目录中唯一的文件。 Should I be saying 我应该说

RAILS_ENV=test bundle exec rspec spec/modules

??? ???

Copying the answer from the comments in order to remove this question from the "Unanswered" filter: 复制评论中的答案,以便从“未回答”过滤器中删除此问题:

I just found a reference that says one cannot load fixtures in before(:all). 我刚刚找到一个引用,说不能在before(:all)中加载灯具。 I'm currently surrounding the test set with a context and do the loading of fixtures within that context before any tests, so this question is now a no-op. 我目前正在用一个上下文围绕测试集,并在进行任何测试之前在该上下文中进行夹具的加载,所以现在这个问题是空话。

~ answer per user1208039 〜每个用户的答复1208039

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM