Tag: rspec

如何使用Rspec测试具有嵌套路由的控制器?

我有两个控制器,我使用铁轨脚手架发电机创建。 我希望它们嵌套在一个名为“demo”的文件夹中,然后运行 rails g scaffold demo/flows rails g scaffold demo/nodes 然后我决定在流内嵌套节点,并改变我的路由文件,如下所示: namespace :demo do resources :flows do resources :nodes end end 但是这种变化导致了对节点的rspec测试破坏了ActionController :: Routing错误。 15) Demo::NodesController DELETE destroy redirects to the demo_nodes list Failure/Error: delete :destroy, :id => “1” ActionController::RoutingError: No route matches {:id=>”1″, :controller=>”demo/nodes”, :action=>”destroy”} 问题是rspec正在寻找错误的路线。 它应该寻找“demo / flows / 1 / nodes”。 它还需要一个模拟流程模型,但我不知道如何提供。 […]

状态机,模型validation和RSpec

这是我当前的类定义和规范: class Event :not_started do event :game_started do transition :not_started => :in_progress end event :game_ended do transition :in_progress => :final end event :game_postponed do transition [:not_started, :in_progress] => :postponed end state :not_started, :in_progress, :postponed do validate :end_time_before_final end end def end_time_before_final return if end_time.blank? errors.add :end_time, “must be nil until event is final” if end_time.present? […]

如何在factory_girl中构建/创建多对多关联?

我有一个与Email模型有多对多关系的Person模型,我想创建一个工厂,让我为该人生成名字和姓氏(已经完成),并创建一个电子邮件地址,基于那个人的名字。 以下是我创建一个person姓名的方法: Factory.sequence :first_name do |n| first_name = %w[FirstName1 FirstName2] # … etc (I’m using a real subset of first names) first_name[(rand * first_name.length)] end Factory.sequence :last_name do |n| last_name = %w[LastName1 LastName2] # … etc (I’m using a real subset of last names) last_name[(rand * last_name.length)] end Factory.define :person do |p| #p.id ??? p.first_name […]

有序列化的领域的工厂女孩

我和工厂女孩有这个问题,它给了我一个undefined method ‘each’ for #错误,来自工厂的序列化字段。 在ActiveRecord中,它运行每个没有问题,因为返回的对象是一个数组。 我的问题是:如何在工厂中格式化序列化对象? 活动记录返回的方式? 或者它实际存储在数据库中的方式? (即序列化或不序列?)rspec会在保存和检索该活动记录时执行相同的序列化魔术吗? 这是我正在做的简化版本: Tvdb.rb–模型 class Tvdb ‘episodes_’ + info.id.to_s, :cache=>request) return request end end 然后在我的Series.rb模型中,我可以这样做: class Series < ActiveRecord::Base def episodes episodes = Tvdb.episodes(self.tvdb_id) episodes.each do |episode| puts episode.name end end end Tvdb.rb – 工厂 FactoryGirl.define do factory :series1_episodes, :class=>Tvdb do term ‘episodes_79488’ cache %q([#,# ) end […]

Rspec / Capybara:测试是否调用控制器方法

鉴于我设置了一个带有索引动作的HomeController class HomeController < ApplicationController def index @users = User.all end end 并通过根路径路由到它, root :to => “home#index” 为什么这个请求规范失败了 it ‘should called the home#index action’ do HomeController.should_receive(:index) visit root_path end 以下消息 Failure/Error: HomeController.should_receive(:index) ().index(any args) expected: 1 time received: 0 times ? 是因为索引方法被调用为实例方法而不是类方法?

ruby 2.1 rails 4个太阳黑子solr测试在套件中失败但是单独通过

我的团队已经在这个问题上被困了一段时间,并且不知道接下来要去哪里尝试。 下面的规范在单独运行时可以正常工作,但是,当我们通过bundle exec ./bin/rspec spec在我们的套件中运行它时,这两个测试每次都会失败: GET / external-products /:id / deals GET /外部产品/搜索/交易 我们已经尝试了许多不同的方法来解决这个问题,我开始怀疑上述规范之外的其他内容。 所以我必须转向堆神,并请求那里有人有更好的方法,甚至更好的问题来问这个问题。 Rspec错误: 8) Retailigence Products and Locations GET /external-products/search/deals Search a given region for related deals by query string Failure/Error: expect(response_body).to have_json_type(Integer).at_path(‘deals/0/id’) JsonSpec::MissingPath: Missing JSON path “deals/0/id” # ./spec/features/external_products_spec.rb:151:in `block (3 levels) in ‘ # -e:1:in `’ 9) Retailigence Products and […]

如何使用rspec测试CLI的stdin

我正在制作一个小型的Ruby程序,无法弄清楚如何编写模拟多个用户命令行输入的RSpec规范(function本身可行)。 我认为这个StackOverflow的答案可能涵盖了最接近我的地方,但这并不是我需要的。 我使用Thor作为命令行界面,但我不认为这是Thor的任何问题。 程序可以从文件或命令行读取命令,并且我已经能够成功编写测试以读取执行它们。 这是一些代码: cli.rb class CLI ” while line = gets break if line =~ /EXIT/i yield [line] print “> ” end end end # .. end 我已经成功测试了使用以下代码执行文件中包含的命令: 投机/ cli_spec.rb describe CLI do let(:cli) { CLI.new } subject { cli } describe “executing instructions from a file” do let(:default_file) { “instructions.txt” } let(:output) […]

RSpec使用正则表达式启动匹配变量

是否存在RSpec start_with匹配器的变体,在检查字符串数组时将使用正则表达式匹配而不是相等? (我不介意自己编写;但我不想重新发明轮子。) 具体来说,我希望有一个看起来像这样的规范: it ‘begins with the standard header’ do output = method_under_test # output is an array of Strings expect(output).to start_with([/foo/, /bar/]) end 如果output[0]匹配/foo/并且output[1]匹配/bar/则此规范应该通过。 假设我确实需要编写自己的匹配器,有没有办法“重载” start_with ,还是需要选择其他名称?

为什么capybara在请求规格中不可用?

使用rspec和capybara处理新的Rails 3.2.9应用程序。 我在Gemfile中有以下内容: gem ‘rspec-rails’ gem ‘capybara’ 以及spec / spec_helper.rb中的以下内容: require ‘rspec/rails’ require ‘capybara/rspec’ 并在spec / requests / asdf_spec.rb中: require ‘spec_helper’ describe ‘Asdf’ do describe “GET /asdfs” do it “should list asdfs” do visit asdfs_path end end end 此测试失败: Failure/Error: visit asdfs_path NoMethodError: undefined method `visit’ for # # ./spec/requests/asdfs_spec.rb:19:in `block (4 levels) in ‘ […]

RSpec找不到Factorygirl的Factorys

我将在我的Rails3项目中使用RSpec和Factory girl。 我已安装工厂女孩,但它没有找到factorys我有这个错误 Failure/Error: Factory.build(:user).should_be valid No such factory: user spec / factories / user_factory.rb: Factory.define :user do |u| u.username ‘otto’ end 投机/ spec_helper.rb ENV[“RAILS_ENV”] ||= ‘test’ require File.expand_path(“../../config/environment”, __FILE__) require ‘rspec/rails’ require ‘factory_girl’ Dir[Rails.root.join(“spec/support/**/*.rb”)].each {|f| require f} RSpec.configure do |config| config.mock_with :rspec config.fixture_path = “#{::Rails.root}/spec/fixtures” config.use_transactional_fixtures = true end 的Gemfile: group :development, :test do […]