Tag: rspec

为什么before_action:authorize以’错误的参数数’失败?

我已经和Devise一起成立了Pundit,以便对我的申请进行授权。 在我的一个控制器中,我有before_action :authorize 。 然后我进行了以下测试: describe SomeController do before(:each) do login_user(FactoryGirl.create(:user, :user_type => :admin)) end describe “GET index” do it “it retrieves the index” do something = FactoryGirl.create(:Something) get :index assigns(:something).should eq([something]) end end end 我收到错误: wrong number of arguments (0 for 1) 登录助手非常简单: module ControllerMacros def login_user(user) if user.nil? user = FactoryGirl.create(:user) end @request.env[“devise.mapping”] […]

具有hstore属性的制造商

我正在尝试使用’制造’构建制造商,’2.8.1’具有hstore属性。 Fabricator(:inventory_item_change) do attribute_changes Hash.new(“num_units” => “to:50”) state “scheduled” change_code 1 execution_at Time.now.advance(days: 3) inventory_item end 这是我使用此制造商运行测试时收到的错误消息。 我已将问题隔离为hstore属性:属性更改。 Failure/Error: attr = Fabricate.attributes_for(:inventory_item_change) ArgumentError: bad value for range 任何人都可以协助识别正确的语法,或其他合适的解决方案来制作具有hstore属性的对象吗?

在RSpec / Rails验收测试中,有没有办法将消息推送到RSpec输出并保持良好的缩进?

我的验收测试(RSpec / Capybara)有一些很长的步骤序列都在一个单独的it…do块下,我想手动将每个步骤的一些附加文档添加到RSpec文档输出中。 那么RSpec输出(文档格式)目前是这样的: FooController New user creates account and creates profile it passes 在很长的步骤序列中,我想将一些额外的信息推送到输出: FooController New user creates account and creates profile … new user saw signup page! … new user got email confirmation! … confirmation link worked! … new user saw empty profile! … new user filled in profile it passes 在记录应用程序方面,这些额外的陈述将比具有单个“传递”结果消息的大黑盒子更好。 由于显然没有办法使用多个it…do块来构建validation测试的长序列步骤,我希望有一种简单的方法可以将其他消息推送到RSpec输出流,理想情况是它们是缩进并显示(红色/绿色),就好像它们被拍或分开it…do例子。

使用FactoryGirl创建的模型在控制器中不可用

原始问题 我正在尝试用RSpec和Capybara编写一些function测试,并想知道为什么我的gon对象总是空的,尽管它是在我的控制器中设置的: app/controllers/timetrackings_controller.rb : class TimetrackingsController false).order(:number) gon.projects = group_by_customer(projects).to_h gon.services = Service.all_cached.select(‘id, name’).where(:archived => false).order(‘LOWER(name)’) end … 现在我想知道为什么这些数据没有呈现到我的视图中(使用save_and_open_page : // ),所以我只是试图在我的测试文件中获取gon值: require ‘spec_helper’ describe ‘the timetracking page’, :js => true do before :each do switch_to_subdomain(‘test’) @project = FactoryGirl.create(:project) @service = FactoryGirl.create(:service) user = FactoryGirl.create(:user) login_as(user, :scope => :user) visit ‘/timetracking’ save_and_open_page end it ‘renders […]

如何在RSpec测试中引发exception

我陷入了测试场景。 我有一个控制器方法: def update @object = Object.find params[:id] # some other stuff @object.save rescue ActiveRecord::StaleObjectError # woo other stuff end 第一部分我测试: context ‘#update’ let(:object) { double } it ‘nothing fails’ do # some other stuff expect(Object).to receive(:find).with(‘5’).and_return(object) expect(object).to receive(:save).and_return(true) xhr :put, :update, id:5 expect(response).to be_success expect(assigns(:object)).to eq(object) end end 现在我想测试ActiveRecord::StaleObjectErrorexception。 我想把它存根,但我没有找到任何解决方法如何做到这一点。 所以我的问题是,如何在RSpec测试中引发ActiveRecord::StaleObjectError ?

如何使FactoryGirl.create影响另一个记录的属性?

在order.save商店应用程序中,通常在预订产品之后, bookings_controller创建操作会执行order.save ,而order.save又会激活必要的before_save方法order.sum_of_all_bookings 。 在为查看订单列表的管理员构建RSpec测试时,进行预订不会更改订单总数。 索引在浏览器中有效。 require ‘rails_helper’ RSpec.feature ‘Admin can oversee orders’ do let(:admin) { FactoryGirl.create(:user, :admin) } let(:customer) { FactoryGirl.create( :user ) } let!(:order_1) { FactoryGirl.create( :order, customer: customer ) } let!(:booking_1) { FactoryGirl.create( :booking, product_name: ‘Honingpot’, product_quantity: 1, product_price: ‘5,00’, order: order_1 ) } let!(:order_2) { FactoryGirl.create( :order, customer: customer ) } […]

如何在测试我的应用程序之前自动加载测试数据库中的数据?

我正在使用Ruby on Rails 3.0.9和RSpec 2.我想在测试我的应用程序之前在测试数据库中自动加载种子数据。 也就是说, 在测试启动时(当我运行测试时),我想“自动填充”“自动启动”测试数据库 。 如何使用种子数据填充数据库? PS :当我读到时,(也许)我应该通过在/spec/spec_helper.rb文件中添加一些代码来填充测试数据库……但是代码和方法是什么? 在我的task/custom.rake文件中,我有: namespace :test do desc “Boot database” task :boot => [:load, :seed] do end desc “Reboot database” task :reboot => [:purge, :boot] do end end

用Rspec绑定File.open

我正在尝试存根File.open以测试我读取CSV文件的方法。 这是模型: class BatchTask def import(filename) CSV.read(filename, :row_sep => “\r”, :col_sep => “,”) end end 这是规范代码: let(:data) { “title\tsurname\tfirstname\rtitle2\tsurname2\tfirstname2\r”} let(:result) {[[“title”,”surname”,”firstname”],[“title2″,”surname2″,”firstname2”]] } it “should parse file contents and return a result” do File.stub(:open).with(“file_name”,”rb”) { StringIO.new(data) } person.import(“file_name”).should == result end 但是,当我尝试这样做时,我得到(stacktrace): Errno::ENOENT in ‘BatchTask should parse file contents and return a result’ No such file […]

Capybara 2.0和rspec-rails – 帮助程序在规范/function中不起作用

我正在尝试使用辅助模块中的方法,但rspec似乎没有识别spec/features下的测试帮助程序。 请注意,对spec_helper.rb的唯一更改是添加spec_helper.rb require ‘capybara/rspec’ 。 我尝试将helper.rb移动到spec/support , spec/helpers和spec/features (包含我的测试的目录),但没有运气。 测试表明辅助方法未定义。 让它“工作”的唯一方法是将我的测试移动到另一个目录,例如spec/integration 。 但现在水豚将无法工作( visit undefined ),因为它不符合spec/features 。 这是我的帮助模块( authentication_helper.rb ): module AuthenticationHelper def sign_in_as!(user) visit ‘/users/sign_in’ fill_in “Email”, with: user.email fill_in “Password”, with: “password” click_button “Sign in” page.should have_content(“Signed in successfully.”) end end RSpec.configure do |c| c.include AuthenticationHelper, type: :request end

Rails邮件程序预览不适用于规格/邮寄/预览

我希望能够在浏览器中预览我的电子邮件。 我正在使用Rspec并在spec / mailers / previews中进行以下预览设置: # Preview all emails at http://localhost:3000/rails/mailers/employee_mailer class EmployeeMailerPreview < ActionMailer::Preview def welcome Notifier.welcome_email(Employee.first) end end 当我尝试访问它时,我收到以下错误: AbstractController::ActionNotFound at /rails/mailers/employee_mailer Mailer preview ’employee_mailer’ not found 但是,当我将测试/邮件/预览中的预览代码放入其中时,它可以工作。 如何配置我的Rails应用程序以查找spec / mailers / previews中的预览?