Tag: rspec

Rspec,Paperclip,Fabrication,有效对象,无需保存到文件系统

我有一个带有paperclip 3.2的rails 3.2 app,我有一个带有所需回形针附件(拇指)的模型。 如何在不将文件保存到文件系统或S3的情况下创建有效对象。 我目前拥有的是下面的内容,但每次运行时都会保存到文件系统中。 有没有办法在没有每次上传的情况下拥有有效的剧集? 模型: class Episode include Mongoid::Document include Mongoid::Paperclip has_mongoid_attached_file :thumb validates_attachment_presence :thumb end 规格: require ‘spec_helper’ describe Episode do it “has a valid factory” do Fabricate.build(:episode).should be_valid end end 制造商: Fabricator(:episode) do thumb { File.open(File.join(Rails.root, ‘spec’, ‘fabricators’, ‘assets’, ‘thumb.jpg’))} end

将自定义目录(源和规范)添加到Rails 3项目中的自动测试

我有一个使用RSpec2作为我的测试框架的Rails 3应用程序,我可以使用自动测试来观察我的模型和spec目录以进行更改,并在文件更改时重新运行我的规范套件。 我想在其中添加一个包含一些自定义类的目录(RAILS_ROOT / lib / some_project / lib / .rb)及其相应的规范(RAILS_ROOT / spec / some_project / _spec.rb),以便自动测试会自动获取更改所有这些文件并根据需要重新运行。 如何让autotest在这些其他目录中观看这些文件? 我怀疑我必须向RAILS_ROOT / autotest / discover.rb添加一些内容,但我不确定该怎么做。

使用RSPEC进行测试时突然出现无法解释的活动记录连接超时

这是我几乎一无所知的地方,所以请提前道歉。 我有一套超过800个rspec测试。 在运行整个集合或仅仅是特定的测试文件时突然而且莫名其妙地,在这些之后(例如20个左右,虽然它们的数字从不完全相同),每个单独的测试都会以相同的错误开始失败: Failure/Error: Unable to find matching line from backtrace ActiveRecord::ConnectionTimeoutError: could not obtain a database connection within 5.000 seconds (waited 5.000 seconds) 在典型的运行中,我会在20次左右的请求测试后开始收到这些错误,其余的780+测试都会因上述完全相同的错误而失败。 我已经尝试回到之前测试完美的先前git提交和分支。 没有运气 – 仍有780多个失败。 还完全删除了重新创建的测试数据库。 也没有运气。 我已经阅读了许多关于连接池等的线程,但我恐怕我不知道如何诊断甚至发生了什么。 以下是我现在所知道的事实: 使用Postgresql 据我所知,开发环境运行良好 在一切工作正常和现在之间,我没有对我所知道的环境进行任何更改/升级。 甚至没有任何数据库迁移。 只需更改模型,视图和控制器代码。 正如我所提到的,回到之前的提交并没有解决任何问题。 config.use_transactional_fixtures = false因为我正在通过Selenium测试ajaxfunction。 但是,无论Selenium是否用于特定的测试集,测试都会失败。 即使我只运行不使用Selenium的测试,在20次左右的测试后仍然会出现故障。 我正在使用具有以下配置的Database Cleaner而不是事务夹具: config.before(:suite) do DatabaseCleaner.clean_with(:truncation) end config.before(:each) do DatabaseCleaner.strategy = :transaction […]

RSpec:如何创建一个帮助存根方法?

我试图从我的帮助器中存取一个辅助方法: # sessions_helper.rb require ‘rest_client’ module SessionsHelper BASE_URL = “http://localhost:1234” def current_user?(token) sessions_url = BASE_URL + “/sessions” headers = {“X-AuthToken” => 12345} begin RestClient.get(sessions_url, headers) return true rescue RestClient::BadRequest return false end end end 我试图存根current_user? 总是在我的unit testing中返回true: require ‘spec_helper’ describe SessionsHelper do it “Should not get current user with random token” do SessionsHelper.stub(:current_user?).and_return(true) expect(current_user?(12345)).to eq(false) […]

在rspec中使用ActiveJob执行挂起作业

我有这个代码用Rspec测试ActiveJob和ActionMailer我不知道如何真正执行所有排队的工作 describe ‘whatever’ do include ActiveJob::TestHelper after do clear_enqueued_jobs end it ‘should email’ do expect(enqueued_jobs.size).to eq(1) end end

对于rails 4.0.1中的新记录,“没有将符号显式转换为字符串”(仅限)

在我的rails 4升级后,尝试为我的任何ActiveRecord类创建一个新记录 No explicit conversion of Symbol into String 例如,这是我的链接links_params方法 def link_params params.require(:link) .permit(:url_address, :group_id, :alt_text, :version_number, :position, :content_date, :verified_date) # This is line 157 end # line 118 is: @link = Link.new(link_params) 但我明白了 TypeError (no implicit conversion of Symbol into String): app/controllers/links_controller.rb:157:in `link_params’ app/controllers/links_controller.rb:118:in `create’ This error occurred while loading the following files: link […]

Rspec渲染文本

我有这个代码 if @temp_user.save sign_in(:user, @temp_user) render text: “OK” else render text: render_to_string(:partial => “errors”) end 我尝试用rspecvalidation渲染“OK” 这是我的实际规格: it “render text OK” do post :create, {:agent => valid_attributes} # response.should have_content(“OK”) response.should render_template(:text => “OK”) end 但这个规范总是会响应0次失败,即使我把“OKI”放到位“OK” 有人对此有一个建议吗?

如何在rails中使用ActiveStorage正确进行模型测试?

我刚刚转向在rails 5.1.4上使用ActiveStorage而且我是TDD的新手并且正在努力弄清楚如何测试has_one_attached :avatar的模型has_one_attached :avatar require ‘rails_helper’ RSpec.describe User, :type => :model do let (:valid_user) { FactoryBot.build(:user) } describe “Upload avatar” do context “with a valid image” do it “saves the image” do valid_user.save! saved_file = valid_user.avatar.attach(io: File.open(“/home/ubuntu/workspace/spec/fixtures/files/avatar.jpg”), filename: “face.jpg”, content_type: “image/jpg”) expect(saved_file).to be_an_instance_of(ActiveStorage::Attachment::One) end end end end 但我收到以下错误: Failures: 1) User Upload avatar with a […]

如何保持rspec测试DRY有很多“have_link”

我是Ruby on Rails的新手,我现在正在做http://ruby.railstutorial.org 。 根据我的理解,该语言应该严格遵循这个DRY标准,但在本教程中涉及到测试驱动开发时,它是如此WET。 例如 it { should have_link(‘Users’, href: users_path) } it { should have_link(‘Profile’, href: user_path(user)) } it { should have_link(‘Settings’, href: edit_user_path(user)) } it { should have_link(‘Sign out’, href: signout_path) } 在这里,我们有很多行几乎看起来一样。 我试过这个 it “should have following links from this array” do [ [‘Users’, href: users_path], [‘Profile’, href: user_path(user)], [‘Settings’, href: […]

在rspec中存根未实现的方法

我正在测试我的模块,我决定测试它与匿名类: subject(:klass) { Class.new { include MyModule } } MyModule在klass使用方法name 。 为了让我的规范工作,我需要存根这个方法name (未实现)。 所以我写道: subject { klass.new } allow(subject).to receive(:name).and_return(‘SOreadytohelp’) } 但它引起了: RSpec::Mocks::MockExpectationError: #<#:0x007feb67c7adf8> does not implement: name from spec-support-3.3.0/lib/rspec/support.rb:86:in `block in ‘ 如何在不定义的情况下存根此方法?