Tag: rspec3

如何在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 ?

使用RSpec 3和Rails 4在控制器测试中查找新创建的记录

我在Rails 4中做了一个控制器规范,我想测试由控制器动作创建的记录的属性。 如何找到新创建的记录? 例如,我可以做什么而不是 it ‘Marks a new user as pending’ do post :create, params # I don’t want to use the following line user = User.last expect(user).to be_pending end Rails指南仅简要讨论了控制器测试,其中提到测试Article.count变化为1,而不是如何获得新的ActiveRecord模型。 在Rails 3中找到最新记录的问题是关于Rails 3。 我不愿意使用User.last ,因为默认排序可能是创建日期之外的其他内容。

在RSpec 3请求中设置标头

我正在尝试为需要身份validation的某些RSpec请求设置标头。 标题是ACCESS_TOKEN 。 无论我如何设置标头,它都永远不会被设置。 我知道应用程序有效,因为我可以手动测试它,我只是不能让rspec测试工作。 请在此处查看此问题的完整源代码和测试: https : //github.com/lightswitch05/rspec-set-header-example 由于在我的大多数请求规范中都使用了身份validation,因此我创建了支持帮助程序模块来检索访问令牌并将其设置在标头中。 下面是我如何设置标题的摘要,看看我在完整源代码中尝试过的所有内容 # my_app/spec/support/session_helper.rb module SessionHelper def retrieve_access_token post api_v1_session_path({email: ‘test@example.com’, password: ‘poor_password’}) expect(response.response_code).to eq 201 expect(response.body).to match(/”access_token”:”.{20}”/) parsed = JSON(response.body) token = parsed[‘access_token’][‘access_token’] @request.headers[‘HTTP_ACCESS_TOKEN’] = token end end 使用此帮助程序的示例请求规范应该可以工作,但始终会失败,因为标头永远不会被设置: # my_app/spec/requests/posts_spec.rb # … context “create” do it “creates a post” do retrieve_access_token post = FactoryGirl.build(:post) […]

获取布尔属性的实际值

我有跨度: Edit Member 当我尝试获取disabled属性的值时: page.in_iframe(:id => ‘MembersAreaFrame’) do |frame| expect(page.span_element(:xpath => “//span[text()=’Edit Member’]”, :frame => frame).attribute(‘disabled’)).to eq(“disabled”) end 我明白了: expected: “disabled” got: “true” 如何获取指定属性的值而不是布尔值?

无法通过last_response读取Rspec 3中的cookie

我试图在Rspec 3.1中读取一个在接到电话后收到的cookie。 我看到它被返回但是last_response.cookies不存在。 我怎样才能阅读回复的cookie? it “doesn’t signs in” do get ‘/ui/pages/Home’ puts last_response.cookies end

用于ActionDispatch路由的Rspec undefined方法’helpers’

我正在将旧的rails 3应用程序升级到rails 5,并在此过程中升级Rspec。 运行测试时,出现错误: NoMethodError Exception: undefined method `helpers’ for # 每当我在响应上调用be_success时。 见例子: it “renders ‘OK’ text when request is html” do get :ping expect(response).to be_success response.body.should == ‘OK’ end 我在运行此测试时遇到错误。 插入断点后,我看到每当调用be_success时都会抛出错误… (byebug) be_success *** NoMethodError Exception: undefined method `helpers’ for # Did you mean? helper_names 我在我的spec_helper文件中包含了config.include Rails.application.routes.url_helpers(使用rails路径路径时遇到了类似的问题),但它对此错误没有帮助。

Aruba不与RSpec 3合作?

我正在尝试测试一个ruby命令行脚本,但在RSpec 3中运行使用无黄瓜Aruba的测试时遇到了麻烦。 一些奇怪的错误,一些明显的错误。 如奇怪的: 1) test does something Failure/Error: run_simple(“cli_test.rb -h”, true) ArgumentError: wrong number of arguments (2 for 1) # …/.gem/ruby/2.2.0/gems/aruba-0.6.2/lib/aruba/api.rb:632:in `assert_exit_status’ # …/.gem/ruby/2.2.0/gems/aruba-0.6.2/lib/aruba/api.rb:750:in `run_simple’ # ./spec/cli_test_spec.rb:17:in `block (2 levels) in ‘ 查看代码,我甚至无法看到是什么触发了这一点。 试图使用Pry或Pry-byebug来检查错误的2个args也没有工作(一大堆其他错误)。 然后,例如明显: 1) test does something Failure/Error: check_file_presence([“bin/cli_test.rb”], true) only the `receive` or `receive_messages` matchers are supported with `expect(…).to`, but you […]

Rspec 3.未定义的局部变量或方法`response’for#

我想用rspec测试API控制器。 所以这是一段简单的代码: require ‘rails_helper’ describe “GET /api/config” do it ‘routes GET to /api/config to Api::V1::ConfigsController#show’ do get ‘/api/config.json’ expect(response).to be_success end end 但我有一个错误: 1) Configs API GET /api/config routes GET to /api/config to Api::V1::ConfigsController#show Failure/Error: expect(response).to be_success NameError: undefined local variable or method `response’ for # # ./spec/routing/api/v1/devise/configs_routing_spec.rb:13:in `block (3 levels) in ‘

Rspec有(n).items未定义的方法

我正在尝试遵循code.tuts的指南,我一直收到错误。 这是我的图书馆规范: require ‘spec_helper’ describe Library do before :all do lib_arr = [ Book.new(“JavaScript: The Good Parts”, “Douglas Crockford”, :development), Book.new(“Dont Make me Think”, “Steve Krug”, :usability), ] File.open “books.yml”, “w” do |f| f.write YAML::dump lib_arr end end before :each do @lib = Library.new “books.yml” end describe “#new” do context “with no parameters” do it […]

在RSpec中,在之前使用let变量:all block

我在大多数测试中都有以下代码: describe ‘index’ let(:company) { FactoryGirl.create(:company) } let(:user) { FactoryGirl.create(:user, company: company) } before do sign_in user visit products_path end … end 但我收到以下警告: WARNING: let declaration ‘user’ accessed in a ‘before(:all)’ 我的问题是,这样做的正确方法是什么? 我找不到有关警告本身的更多信息。 谢谢! 编辑:我的目标是使用用户变量,以便我可以将其传递给sign_in,签名用户,然后在另一个测试中使用它(我检查用户的公司属性)