Tag: rspec

RSpec测试使用GET参数重定向到URL

假设我有一个带有redirect_to_baz方法的FoosController 。 class FoosController < ApplicationController def redirect_to_baz redirect_to 'http://example.com/?foo=1&bar=2&baz=3' end end 我正在使用spec/controllers/foos_controller_spec.rb测试它: require ‘spec_helper’ describe FoosController, :type => :controller do describe “GET redirect_to_baz” do it “redirects to example.com with params” do get :redirect_to_baz expect(response).to redirect_to “http://example.com/?foo=1&bar=2&baz=3” end end end 有用。 但是,如果有人交换查询字符串参数(例如http://example.com/?bar=2&baz=3&foo=1 ),则测试失败。 测试这个的正确方法是什么? 我想做的事情如下: expect(response).to redirect_to(“http://example.com/”, params: { foo: 1, bar: 2, baz: 3 […]

如何使用RSpec正确测试CanCan能力

我是第一次测试CanCan的能力而且很难过。 我错过了一些东西……即使我在can中返回false / true:invite_to block我仍然没有通过规范。 我错过了使用CanCan匹配器吗? 还是存根? 或者在CanCan中定义能力? 我错过了什么? ability.rb class Ability include CanCan::Ability def initialize(user) user ||= User.new can :invite_to, Network do |network| network.allows_invitations? && (user.admin? || user.can_send_invitations_for?(network)) end end end ability_spec.rb require ‘cancan’ require ‘cancan/matchers’ require_relative ‘../../app/models/ability.rb’ class Network; end; describe Ability do let(:ability) { Ability.new(@user) } describe “#invite_to, Network” do context “when […]

在每个示例之后,factory_girl + rspec似乎没有回滚更改

与此处描述的问题类似: http : //rpheath.com/posts/411-how-to-use-factory-girl-with-rspec 简而言之(缩短代码): spec_helper: config.use_transactional_fixtures = true config.use_instantiated_fixtures = false factories.rb: Factory.define :state do f.name “NY” end 在我的规范中 before(:each) do @static_model = Factory(:state) # with validate uniqueness of state name end 错误: 重复的条目名称“NY”等。 问题:在每个规范示例之前,不应该rspec清除数据库,因此不会抛出重复的条目错误吗?

在Ruby中重置单例实例

如何在Ruby中重置单个对象? 我知道在实际代码中我们永远不想这样做但是unit testing呢? 这是我在RSpec测试中尝试做的事情 – describe MySingleton, “#not_initialised” do it “raises an exception” do expect {MySingleton.get_something}.to raise_error(RuntimeError) end end 它失败了,因为我之前的一个测试初始化​​了单例对象。 我试过从这个链接开始关注Ian White的建议,它基本上是猴子补丁Singleton来提供reset_instance方法,但我得到一个未定义的方法’reset_instance’exception。 require ‘singleton’ class <<Singleton def included_with_reset(klass) included_without_reset(klass) class <<klass def reset_instance Singleton.send :__init__, self self end end end alias_method :included_without_reset, :included alias_method :included, :included_with_reset end describe MySingleton, "#not_initialised" do it "raises an exception" […]

ActionDispatch :: Http :: UploadedFile.content_type未在Rspec测试中初始化

背景 :我有一个带有cover_file属性的Book模型,该属性通过我的一个Rails控制器使用上传的文件进行设置。 我正在使用Rails v4.0.4。 目标 :我想测试只保存具有特定内容类型的文件。 我计划使用ActionDispatch::Http:UploadedFile创建Rspec测试示例ActionDispatch::Http:UploadedFile使用不同的content_type属性设置ActionDispatch::Http:UploadedFile对象。 问题 :当我使用content_type初始化一个新的ActionDispatch::Http::UploadedFile时,似乎没有设置(请参阅下面的test&output确认它是nil)。 似乎我只能在初始化UploadedFile之后用setter设置它。 我没有在文档中看到任何提及这种行为,也没有在SO上找到类似的问答,所以我很感激任何人帮助确定我做错了什么。 谢谢! 代码 : describe Book do let(:book) {FactoryGirl.build(:book)} describe “Save” do context “with valid data” do before do cover_image = File.new(Rails.root + ‘spec/fixtures/images/cover_valid.jpg’) book.cover_file = ActionDispatch::Http::UploadedFile.new(tempfile: cover_image, filename: File.basename(cover_image), content_type: “image/jpeg”) puts book.cover_file.content_type.nil? book.cover_file.content_type = “image/jpeg” puts book.cover_file.content_type end specify{expect(book.save).to be_true} end end end […]

用水豚进行平行selenium试验

背景: 我有一组针对我的Rails 3应用程序运行的Capybara集成测试。 对于测试套件的其他部分,我正在使用Rspec 。 我的Mac OSX开发机器上有一个selenium 2.6.0独立服务器集线器。 java -jar selenium-server-standalone-2.6.0.jar -role hub 我正在运行几个虚拟机,每个虚拟机都与一个selenium节点连接到集线器: java -jar selenium-server-standalone-2.6.0.jar -role webdriver -hub http://0.0.1.12:4444/grid/register port 5555 -browser browserName=”internet explorer”,version=8,platform=WINDOWS 这很好用,在这个截图中,控制台显示我有一个连接到集线器的IE7和IE8浏览器: 我已经设置了capybara来对抗selenium hub(将测试委托给节点)。 Capybara.app_host = “myapp.dev” Capybara.default_driver = :selenium Capybara.register_driver :selenium do |app| Capybara::Selenium::Driver.new(app, :browser => :remote, :url => “http://localhost:4444/wd/hub”, :desired_capabilities => :internet_explorer) end 它可以工作,但它只会在单个internet_explorer节点上运行测试。 它似乎是“排在第一位”的那个; 如果我将其关闭,测试将在另一个节点上成功运行。 我一直在尝试parallel_tests项目,按照建议配置capybara,但这仍然只会启动一个集成测试。 如何同时在所有internet_explorer节点上运行我的集成? […]

rspec测试has_many:through和after_save

我有一个(我认为)相对简单的has_many :through与连接表的关系: class User :user_following_thing_relationships end class Thing :user_following_thing_relationships, :source => :user end class UserFollowingThingRelationship < ActiveRecord::Base belongs_to :thing belongs_to :user end 这些rspec测试(我知道这些不一定是好的测试,这些只是为了说明发生了什么): describe Thing do before(:each) do @user = User.create!(:name => “Fred”) @thing = Thing.create!(:name => “Foo”) @user.things << @thing end it "should have created a relationship" do UserFollowingThingRelationship.first.user.should == @user UserFollowingThingRelationship.first.thing.should == @thing […]

将标头附加到Rspec控制器测试

我正在尝试为我的控制器编写测试,以接收来自外部服务的请求。 到目前为止,这是我的测试: describe ApplyController do context ‘when valid’ do let(:parameters) do file = File.join File.dirname(__FILE__), ‘..’, ‘samples’, ‘Indeed.json’ JSON.parse(File.read file) end let(:signature) { ‘GC02UVj0d4bqa5peNFHdPQAZ2BI=’ } subject(:response) { post :indeed, parameters, ‘X-Indeed-Signature’ => signature } it ‘returns 200 ok if Request is valid’ do expect(response.status).to eq 200 end end end 这应该根据我能找到的文档工作。 我的控制器现在看起来像这样: class ApplyController < Application […]

为什么我不建议“在全球范围内包括Capybara :: DSL!”

每次运行规范时,即使规范通过,例如 $ rspec spec/integration/view_homepage_spec.rb including Capybara::DSL in the global scope is not recommended! . Finished in 0.6174 seconds 1 example, 0 failures Randomized with seed 14130 $ 我的Gemfile有: group :test, :development do gem ‘rspec-rails’ gem ‘capybara’ end 我的spec_helper有: ENV[“RAILS_ENV”] ||= ‘test’ require File.expand_path(“../../config/environment”, __FILE__) require ‘rspec/rails’ require ‘rspec/autorun’ require ‘capybara’ include Capybara::DSL Dir[Rails.root.join(“spec/support/**/*.rb”)].each { |f| […]

RSpec:我如何编写一个期望某些输出但不关心该方法的测试?

我试图了解测试驱动设计,特别是RSpec。 但是我遇到了RSpec Book的一些例子。 在本书中,我们测试$ STDOUT的输出如下: output = double(‘output’) game = Game.new output.should_receive(:puts).with(‘Welcome to Codebreaker!’) game.start() 嗯,这是一种时尚后的作品。 但是,为什么我应该关心Game对象是否使用puts()方法? 如果我把它改成print(),它真的会破坏测试吗? 而且,更重要的是,这不是针对TDD的主要原因之一 – 我应该测试该方法的作用(设计)而不是它是如何做的(实现)? 有没有什么方法可以编写一个测试,只测试最终在$ STDOUT上的内容,而不用看看它放在哪个方法?