Tag: rspec

如何设计一个保持SOLID原则和设计模式的应用程序

在启动时说ruby中的应用程序有两种模式:命令行模式和文件模式 给定参数ruby myprogram input.txt output.txt时 ,它会根据输入文件中的某些命令生成输出。 当没有提供任何参数时,它会向我们提供命令提示符。 使用以下命令。 create_class_with_capacity 40 create_student_with_marks Alex 70 create_student_with_marks Mathew 30 create_student_with_marks John 55 .. create_student_with_marks Sylvia 70 etc… fail_student_roll_no 12 => Student with roll number 12 #{student} failed give_marks_to_roll_no 70 1 =>Student with roll number 1 Alex got 70 marks find_all_students_with_marks 70 => Alex, Peter , Russell , Mark […]

使用Serverspec进行Docker映像构建测试,检查空gems缓存失败

我目前遇到的问题是通过Serverspec测试Docker镜像构建。 简而言之,我想要做的是确保在图像构建期间显式清除Ruby gems构建缓存,例如通过在Dockerfile中发出rm -rf /usr/lib/ruby/gems/*/cache/*.gem 。 我正在使用的Dockerfile骨架如下所示: # Dockerfile FROM alpine:3.7 RUN apk add –no-cache \ dumb-init \ ruby \ && apk add –no-cache –virtual .build-deps \ build-base \ ruby-dev RUN gem install –no-rdoc –no-ri json \ && gem install –no-rdoc –no-ri oj RUN apk del .build-deps \ && rm -rf /var/cache/apk/* \ /tmp/* /var/tmp/* […]

FactoryGirl循环依赖和validation

我是FactoryGirl的新手,在Ruby中,我正在尝试编写一些rspec但是我遇到了一些循环依赖的麻烦。 我搜索并尝试了几个东西,最后一个产生了无限循环,我不得不关闭我的WM。 (proc 100%) 车型/投票 class Vote < ActiveRecord::Base has_many :vote_options 车型/ vote_option class VoteOption < ActiveRecord::Base belongs_to :vote accepts_nested_attributes_for :vote_options, reject_if: :all_blank 规格/型号/ vote_spec describe Vote do describe ‘should return the good label’ do let(:nobody) { FactoryGirl.create(:vote, visibility: 0) } it “should not be visible” do nobody.intercom_custom_data[:visibility].should == I18n.t(“votes.new.visibility_none”) end end end 规格/工厂。 factory […]

使用RSpec测试Datamapper模型

我正在使用RSpec测试一个使用DataMapper的Sinatra应用程序。 以下代码: it “should update the item’s title” do lambda do post “/hello/edit”, :params => { :title => ‘goodbye’, :body => ‘goodbye world’ } end.should change(Snippet, :title).from(‘hello’).to(‘goodbye’) end 导致此错误: title应该最初是“hello”,但是#DataMapper :: Property :: String @ model = Snippet @ name =:title> 我当然可以通过删除lambda来解决这个问题,并且只检查是否: Snippet.first.title.should == ‘goodbye’ 但这不是一个长期解决方案,因为.first Snippet在未来可能不一样。 有人能告诉我正确的语法吗? 谢谢。

无法通过last_response读取Rspec 3中的cookie

我试图在Rspec 3.1中读取一个在接到电话后收到的cookie。 我看到它被返回但是last_response.cookies不存在。 我怎样才能阅读回复的cookie? it “doesn’t signs in” do get ‘/ui/pages/Home’ puts last_response.cookies end

在test.rb中加载的变量在测试中找不到

我正在运行rspec 2.5.1,ruby 1.9.2和rails 3.0.5 我移动了一些设置,用于将邮件发送到yaml文件中,我在environment.rb中加载: APP_CONFIG = YAML.load_file(“#{RAILS_ROOT}/config/config.yml”) 邮件程序类是这样的: class Notifier APP_CONFIG[‘support_email’] … end 这在开发中效果很好,但是在运行任何测试之前,rspec会咳嗽一个毛球: /…/rspec/core/backward_compatibility.rb:20:in ‘const_missing’: uninitialized constant Notifier::APP_CONFIG (NameError) from /rspec/expectations/backward_compatibility.rb:6:in ‘const_missing’ from /…/app/mailers/notifier.rb:2:in ” 我没有运行spork或类似的东西,所以我认为必须加载rails环境才能运行测试? 任何帮助弄清楚我搞砸了什么都会很棒。 如果我发布代码的任何其他部分,请在评论中告诉我,谢谢。

如何打破RSpec超长规格?

有没有一种方法可以将模型的规格分解为某种层次结构? 我们的几个型号的规格已超过5000线,这降低了它们的可维护性。 我知道在一个理想的世界中,模型不够复杂,不需要这么大的测试,但这些模型实际上是来自Python科学库(系列和数据框架)的pandas对象的Ruby实现,所以我们不幸受此限制。 我希望能够通过逻辑运算来分割测试。 这可能吗? 理想情况下,我还希望使用单个命令运行模型的所有规范。

使用Rspec测试Sidekiq工作人员

我如何测试以下代码行使用rspec在5分钟内调用’perform’? CustomSidekiqWorker.perform_in(5.minutes, parameter1)

使用FileApi将文件上载到输入区域

我正在使用Capybara + RSpec和capybara_webkit作为驱动程序。 和一个带有FileApi的JS上传器。 我正在尝试上传三张图片: 当我点击“选择照片”按钮(在浏览器上)时,它会打开一个典型的窗口,我可以从我的计算机中选择3个文件。 我想知道如何在水豚上重现它,就像照片选择器打开时一样,我无法控制它。 我试图将图片添加到我的测试文件夹并尝试: attach_file(‘image’, File.absolute_path(‘../pictures/photo1.JPG’)) 但没有结果。

没有rails的RSpec没有加载spec / support中的文件

在文件spec / support / factory_girl.rb中我有 fail “At least this file is being required” RSpec.configure do |config| config.include FactoryGirl::Syntax::Methods end 我有一些规范代码 require ‘pmc_article’ require ‘pmc_article_parser’ require ‘factory_girl’ require_relative ‘./factories/pmc_article_factory’ RSpec.describe PMCArticle do let(:pmc_article) do build(:pmc_article) end it ‘parses pmid’ do expect(pmc_article.article_id_pmid).to eq ‘123456’ end end 但是当我运行bundle exec rspec我得到了 1) PMCArticle parses pmid Failure/Error: build(:pmc_article) NoMethodError: undefined […]