Tag: rspec

Paperclip + RSpec:content_typevalidation

我在我的Rails应用程序中使用Paperclip来附加images 。 我在我的模型中声明了对content_typevalidation validates_attachment :image, :content_type => { :content_type => [“image/jpg”, “image/gif”, “image/png”] } 我有两个例子,一个是有效图像,另一个是图像无效 对于无效图像,我只是将.txt文件重命名为.png it “Image is valid” do image = File.new(“#{Rails.root}/spec/support/right.png”) expect(FactoryGirl.build(:pin, image: image)).to be_valid end it “Image is invalid” do image = File.new(“#{Rails.root}/spec/support/wrong.png”) expect(FactoryGirl.build(:pin, image: image)).to have(1).errors_on(:image_content_type) end 我希望我的两个例子都能成功运行。 但是,我的第二个例子失败了。 我没有得到wrong.png content_type的任何错误。 我认为Paperclip的content_typevalidation实际上会检查上传文件的文件格式(二进制数据编码)。 但似乎在这里,它只是检查文件扩展名。 此validation仅检查上传文件的扩展名吗? 我可能在这里遗漏了一些东西(配置?)。 Paperclip中是否还有其他validation可用于实现此目的? 或者在这种情况下我应该选择自定义validation器吗?

FactoryGirl瞬态变量的未定义方法

我在version 4.4使用FactoryGirl 。 我正在尝试创建after(:create)回调,但我在评估变量上失败了。 FactoryGirl.define do factory :organization do name ‘example’ factory :organization_with_links do transient do links_count 5 end after(:create) do |organization, evaluator| create_list(:link, evaluator.links_count, organization: organization) end end end end 不幸的是我得到了 NoMethodError: undefined method `links_count’ for FactoryGirl::SyntaxRunner:0x007fcefb1ff780> 根据官方工厂女孩指南我正在做的一切正确: https : //github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#associations 任何建议如何使transient variable工作?

Rspec测试应该通过但是失败

我从迈克尔哈特书中得到了这个测试: require ‘spec_helper’ describe “Static pages” do let(:base_title) { “Ruby on Rails Tutorial Sample App” } describe “Home page” do it “should have the h1 ‘Sample App'” do visit ‘/static_pages/home’ page.should have_selector(‘h1’, :text => ‘Sample App’) end it “should have the title ‘Home'” do visit ‘/static_pages/home’ page.should have_selector(‘title’, :text => “#{base_title} | Home”) end end […]

Postgres的观点未被rspec认可

使用Rails 3.2.18和Postgres,我有一些表,我已经为其创建了一个视图,以最小化ActiveRecord加载的数据。 在应用程序中,一切似乎都正常运行,但当我尝试运行Rspec时,我得到: ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation “properties_view” does not exist (’properties_view’是’properties’表中的视图) 我该怎么做才能确保Rspec从Postgres正确加载视图?

如何在错误的rspec规范之间清理我的数据库?

我已将database_cleaner gem添加到我的rails应用程序中,以便在规范之间清理我的数据库。 这是我对database_cleaner的当前配置,位于spec/spec_helper.rb : config.before(:suite) do DatabaseCleaner.strategy = :transaction DatabaseCleaner.clean_with(:truncation) DatabaseCleaner.start DatabaseCleaner.clean end config.before(:each) do DatabaseCleaner.clean end config.after(:each) do DatabaseCleaner.clean end config.after(:suite) do DatabaseCleaner.clean end 现在,只要运行的每个最后一个规范通过或失败,此配置都可以正常工作。 但是,如果出现错误(rspec没有给你一个漂亮的小E像minitest,它会引发这样的事情: 09:17:32 – INFO – Running: spec /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/gems/1.9.1/gems/activerecord-4.0.1/lib/active_record/validations.rb:57:in `save!’: Validation failed: Email has already been taken (ActiveRecord::RecordInvalid) ),数据库没有清理! 错误之前的规范中的残留数据保留在数据库中。 我想这是因为database_cleaner不会将错误的规范视为完成,因此不会清理数据库。 现在,在您再次运行规范之前,这并不会造成任何伤害。 然后残差数据会产生类似于此的错误: 09:17:32 – INFO – Running: spec /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/gems/1.9.1/gems/activerecord-4.0.1/lib/active_record/validations.rb:57:in […]

为什么Capybara在一次活动后丢弃我的会话?

测试具有before_filter :authenticate_user!的rails应用程序before_filter :authenticate_user! 对于大多数控制器,我无法让Capybara保留会话。 我使用PhantomJS与恶作剧者配置了Capybara。 我使用以下帮助器: require ‘spec_helper’ include Warden::Test::Helpers module FeatureHelpers def login(user = FactoryGirl.create(:default_user)) login_as user, scope: :user user end end 我有以下spec文件: require ‘spec_helper’ include Warden::Test::Helpers feature ‘Leads Data Tasks View’ do before(:each) do @user = login end after{ Warden.test_reset! } context “clicking a task button” do scenario “login persists across multuple actions”, […]

当我使用rspec:无效的路由名称,已经在使用:’root’(ArgumentError)

当我使用rspec进行一些测试时: bundle exec rspec spec/ terminal会向我抛出此错误。 但在我的route.rb中,我认为我只定义了一个根路径 CalebdeMacBook-Pro:sample_app Caleb$ bundle exec rspec spec/ /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.3/lib/action_dispatch/routing/route_set.rb:434:in `add_route’: Invalid route name, already in use: ‘root’ (ArgumentError) You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the […]

Ruby Object#id警告和Active Record

当我们运行我们的规范时,我们会不断看到如下警告: Object#id将被弃用; 使用Object#object_id 有问题的代码是访问ActiveRecord模型的id(显然,这是表中的属性,而不是Ruby VM中的对象实例ID)。 有谁知道如何关闭这些特定的警告或以某种方式避免它们?

如何从引擎中使用FactoryGirl工厂

我们有两个共享相同数据库和模型的Web应用程序。 为了保持DRY,我们创建了一个包含所有模型的引擎。 此引擎通过Gemfile包含在相应的应用程序中。 我们确实在引擎内部使用RSpec和FactoryGirl进行了测试。 现在我们正在尝试编写集成测试,我们希望使用引擎中的工厂而无需复制或符号链接。 有没有办法以编程方式实现这一目标?

Rspec / FactoryGirl:清理数据库状态

我是Rspec和Factory girl的新手,希望我的测试能够在特定的数据库状态下运行。 我知道我可以让Factory girl创建这些记录,并且在测试运行后对象将被销毁,但是如果我在数据库中有数据会发生什么。 例如:我希望我的测试在我通过Factory Girl创建的数据库中有3条记录时运行。 但是,我目前在数据库中已有1个模型记录,我不想仅为测试删除它。 在那里有一个模型毁了我的测试。 数据库内容 [#] leaderboard_spec.rb require ‘spec_helper’ describe Rom::Leaderboard do describe “poll leaderboard” do it “should say ‘Successful Run’ when it returns” do FactoryGirl.create(:leaderboard, score: 400, name: “Alice”) FactoryGirl.create(:leaderboard, score: 300, name: “Bob”) FactoryGirl.create(:leaderboard, score: 200, name: “John”) Leaderboard.highest_scorer.name.should == “Alice” end end end 现在我的测试将失败,因为它会错误地认为Trudy是最高得分手,因为测试运行的状态不正确。 工厂女孩是否提供从数据库中删除记录然后回滚此删除? 类似于它在数据库中创建记录和回滚的方式