Tag: rspec

使用fixture_file_upload方法Rails 4 Rspec

我正在使用FactoryGirl和Rspec测试我的Image对象的创建。 我正在使用我正在使用的 photo { File.new(File.join(Rails.root, ‘spec/fixtures’, photo_name)) } 但我突然间得到了 Paperclip::AdapterRegistry::NoHandlerError: No handler found for “# 所以在阅读各种post之后我已经改为fixture_file_upload方法,但是在创建对象时遇到了麻烦。 images.rb include ActionDispatch::TestProcess FactoryGirl.define do factory :image do transient do photo_name ‘validated_image.jpg’ end photo { fixture_file_upload(Rails.root.join(‘spec’, ‘fixtures’, photo_name), ‘image/jpeg’) } end 虽然最初我试过这个,因为我的rails_helper声明了夹具路径 photo { fixture_file_upload photo_name, ‘image/jpeg’ } # config.fixture_path = “#{::Rails.root}/spec/fixtures” # rails_helper # Would produce error ‘validated_image.jpg […]

Capybara不会填写表格字段(虽然它找到了)

我正在使用Ruby 2.2.1和Capybara 2.2.4开发Rails 4.2.1。 编写规范来注册用户,Capybara确实找到了字段(我没有得到ElementNotFound错误),但是当我尝试validation时,我发现所有字段都是空的并且在打开launchy之前告诉Capybara单击注册按钮显示所有字段为空。 规格: require ‘rails_helper’ RSpec.feature “Sign Up Users”, type: :feature do scenario “saves user to the database with valid data” do visit root_path click_link “Sign Up” find(“#user_name”).set(“John Doe”) fill_in “user_username”, with: “johndoe” fill_in “user_email”, with: “john@example.com” fill_in “user_password”, with: “helloworld” fill_in “user_password_confirmation”, with: “helloworld” save_and_open_page click_button “Create Account” expect(page).to have_text(‘Account successfully […]

RSpec – 无法写入未知属性(枚举)

型号用户: class User < ActiveRecord::Base enum my_enum: [ :some_value1, #…. ] 我还有一个迁移,它将my_enum添加到用户: def change add_column :users, :my_enum, :integer end FactoryGirl的夹具: FactoryGirl.define do factory :user do email { Faker::Internet.email } password { Faker::Internet.password(10) } password_confirmation { password } my_enum { nil } end end 一切正常。 但是当我运行测试时,我收到一个错误: Failure/Error: paid_user = FactoryGirl.create(:user) ActiveModel::MissingAttributeError: can’t write unknown attribute `my_enum`

rspec的.should在Ruby 2中失败(在describe / it块之外)?

在Ruby 2中,使用gem rspec 2.14.1(Ubuntu的最新版本),未安装Rails,为什么会失败? require ‘rubygems’ require ‘rspec’ 3 .should == 3 NoMethodError: undefined method `should’ for 3:Fixnum 多年来我一直依赖方便的习语x .should == y 。 https://www.relishapp.com/rspec/rspec-expectations/v/2-14/docs/syntax-configuration和https://www.relishapp.com/rspec/rspec-expectations/docs/syntax-configuration say默认情况下仍支持此语法。 编辑:在标题中添加“outside describe / it block”,因为这似乎是根本原因。

在RSpec中捕获STDOUT

我是Ruby的unit testing的新手,我在创建一个检查STDOUT的规范时遇到了一些麻烦。 我知道如果我正在测试的代码是在类/方法中,如何测试STDOUT,但我想测试一个来自常规Ruby文件的puts语句,该文件不属于Class或Method。 这是来自my_file.rb的简单一行 puts “Welcome to the Book Club!” 这是规格 my_spec.rb require_relative ‘my_file.rb’ describe “Something I’m Testing” do it ‘should print Welcome to the Book Club!’ do expect {$stdout}.to output(“Welcome to the Book Club!\n”).to_stdout end end 我收到以下错误消息: Failures: 1) Something I’m Testing should print Welcome to the Book Club! Failure/Error: expect {$stdout}.to output(“Welcome to […]

存根和rspec旧语法的问题

我正在编写一些代码并使用rspec,但收到一个警告,语法是旧的,我不知道我应该如何编写它? it “should calculate the value correctly” do mock_cards = [Card.new(:clubs, 5), Card.new(:diamonds, 10)] hand = Hand.new hand.stub(:cards) { cards } #stub out cards and have it return cards expect(hand.value).to eq (15) end 错误消息如下:使用来自rspec-mocks’old的stub :should不使用显式启用语法的语法。 使用new :expect语法或显式启用:should改为。

ruby:无法使用带有rspec的记录器

我正在尝试将日志记录添加到简单的rspec测试中。 我正在使用Watir在规范中驱动Chrome,这很好用。 我无法使用“Logger”库获取日志。 这是我的规格: require ‘rubygems’ require ‘watir-webdriver’ require ‘rspec’ require ‘logger’ describe ‘ui tests’ do let(:browser) { browser ||= Watir::Browser.new :chrome } let(:log) { log = Logger.new(STDOUT) log = Logger.new(‘watir.tests.log’, ‘daily’) log.level = Logger::DEBUG } before { browser.goto ‘http://translate.google.com’ } after { browser.close } context ‘simple tests’ do it ‘simple test’ do log.info(“Running simple […]

如何使用rspec测试multithreadingTCPServer

我写了一个非常简单的日期服务器: require ‘socket’ s = TCPServer.new 3939 while (conn = s.accept) Thread.new(conn) do |c| c.print “Enter your name: ” name = c.gets.chomp c.puts “Hi #{name}, the date is…” c.print `date` c.close end end 用户连接,产生一个线程,他们输入他们的名字,返回日期。 简单。 我想知道如何在rspec中测试这样的东西。 我有过的一些想法:1。)使用VCR记录服务器连接,使用Timecop冻结并返回日期。 2.)在之前的块中连接到实际的服务器。 我不完全确定如何执行此操作,因为当我运行rspec时,我认为它实际上运行服务器…或者发生了一些事情,终端只是冻结并等待某些东西……示例测试代码: before @server = TCPSever.new 3939 end it “does something..” conn = @server.accept # etc end after […]

使用FactoryGirl构建json存根

我尝试使用工厂构建一个json ,但是当我尝试build它时它是空的。 以下是Factory类。 require ‘faker’ FactoryGirl.define do factory :account do |f| f.name {Faker::Name.name} f.description {Faker::Name.description} end factory :accjson, class:Hash do “@type” “accountResource” “createdAt” “2014-08-07T14:31:58” “createdBy” “2” “disabled” “false” end end 以下是我试图建立的方式。 hashed_response = FactoryGirl.build(:accjson) expect(account.to_json).to eq(hashed_response.to_json); 但我的hashed_response似乎总是空object 。

在rspec中的控制器测试中使用rails“post”,并在路由上使用作用域和协议

我有一个rails项目,它正在生产中基本URL的子目录中运行,我希望它在dev中以这种方式运行,以使dev和prod尽可能接近。 我有路由文件设置如下: Foo::Application.routes.draw do def draw_routes root :to=>’foo#home’ resources :cats, :only=>[:create] do end if Rails.env.production? scope :protocol=>’http://’ do draw_routes end else scope :path=>”/foo”, :protocol=>’http://’ do draw_routes end end end 我的CatsController是这样的: class CatsController “Bob”) if @cat.save() redirect_to root end end end 我想测试我的Create Cat方法,所以我在spec / controllers / cats_controller_spec.rb中设置了一个rspec测试: require ‘spec_helper’ describe CatsController do describe “calling create cat […]