Tag: rspec

Rails:RSpec – 用于nil的未定义方法`cookie_jar’:NilClass

Rails新手。 试着按照Michael Hartl的教程。 试图添加辅助方法来模拟RSpec测试中的日志: describe “when the a user has logged in and attempts to visit the page” do let(:user) { FactoryGirl.create :user } before do log_in user end it “should redirect the user to next page” do specify { response.should redirect_to loggedin_path } end end 在我的spec / support / utilities.rb中: def log_in user visit […]

rspec用户测试给出“未定义的局部变量或方法`confirmed_at’”

我的rspec测试给了我 NameError: undefined local variable or method `confirmed_at’ for # 我的用户规范是: require ‘spec_helper’ describe User do before(:each) do @user = Factory(:user) end # Factory will make sure that in the future if attributes are added the tests below don’t break # Just as long as the Factory is updated for the new attributes as appropriate. […]

如何使用rspec模拟AWS SDK(v2)?

我有一个类使用aws-sdk-rails gem (它是aws-sdk-ruby v2的包装器)从SQS队列读取/处理消息。 如何模拟AWS调用,以便我可以在不调用外部服务的情况下测试我的代码? communicator.rb : class Communicator def consume_messages sqs_client = Aws::SQS::Client.new # consume messages until the queue is empty loop do r = sqs_client.receive_message({ queue_url: “https://sqs.region.amazonaws.com/xxxxxxxxxxxx/foo”, visibility_timeout: 1, max_number_of_messages: 1 }) break if (response.message.length == 0) # process r.messages.first.body r = sqs_client.delete_message({ queue_url: “https://sqs.region.amazonaws.com/xxxxxxxxxxxx/foo”, receipt_handle: r.messages.first.receipt_handle }) end end end

RSpec:如何设置控制器的实例变量

我的rails控制器中有这个方法: def some_init_func # … @inst_var = 1 # … end 后来: do_something_with(@inst_var) 如何在RSpec中设置此实例变量? allow(controller).to receive(:some_init_func) do # ???? what goes here ???? end Muchas gracias 🙂

运行规范时出现的描述性错误,取决于种子

我编写了一个function规范来测试我的应用程序中的正确多租户行为。 它以两个不同的用户身份登录,通过填写表单并将其作为每个用户提交来创建新的Presentation ,并在每次只有租户自己的演示文稿可见时确认。 这个规范有时很好,有时也没有。 真正令人困惑的是发生的错误: Failure/Error: click_button “Präsentation erstellen” TypeError: can’t cast ActiveSupport::HashWithIndifferentAccess to # ./app/controllers/presentations_controller.rb:21:in `create’ # ./spec/features/presentation_management_spec.rb:22:in `block (3 levels) in ‘ 并且这不是粘贴时的错误,它字面上说can’t cast ActiveSupport::HashWithIndifferentAccess to并停在那里。 上下文中引用的行: ./app/controllers/presentations_controller.rb 18 def create 19 @presentation = Presentation.new(presentation_params) 20 21 if @presentation.save 22 flash_for(@presentation, :create) 23 redirect_to @presentation 24 else 25 render :new 26 end 27 […]

选择带有Rspec的单选按钮

我正在使用RSpec为表单编写测试,我想知道如何选择单选按钮,给出以下表单: 我希望我的测试看起来像: describe “with valid options selected” do before do #CODE TO SELECT A RADIO BUTTON click_button “SAVE” end end

在rspec中模拟错误/exception(不仅仅是它的类型)

我有一个像这样的代码块: def some_method begin do_some_stuff rescue WWW::Mechanize::ResponseCodeError => e if e.response_code.to_i == 503 handle_the_situation end end end if e.response_code.to_i == 503部分,我想测试一下发生了什么。 我可以模拟do_some_stuff来抛出正确的exception类型: whatever.should_receive(:do_some_stuff).and_raise(WWW::Mechanize::ResponseCodeError) 但是当我收到“response_code”时,如何模拟错误对象本身返回503?

使用RSPec进行模拟/存根系统调用

这是辅助模块: module SendingMailHelper def send_mail(subject, body) to_email_id = ” abc@gmail.com” cc_email_id = “def@gmail.com” html_message = %{#{body}} flag=false while(!flag) do flag = system %{echo “#{html_message}” | mutt -e “set content_type=text/html” -s “#{subject}” #{cc_email_id} — #{to_email_id}} end flag end end 我编写规范的方式如下,但它不起作用: require File.expand_path(File.dirname(__FILE__) + ‘/../spec_helper’) describe SendingMailHelper do it “test something” do html_message = %{www.google.com} to_email_id = […]

如何在Capybara中使用同步?

如果使用wait_until非常清楚(我在使用本机Webdriver方法创建测试时使用了这样的方法),而不是新的同步方法(抱歉:))。 我已经阅读了关于为什么不推荐使用wait_until的主题,我已经阅读了有关该文章的文章,我已经阅读了带有方法描述的文档,并且还阅读了描述所在的代码。 但我没有找到任何示例或教程如何使用此方法。 请允许任何人提供我(也可能是其他人)可以看到并学习如何使用此方法的少数情况 例如案例 expect(actual).to equal(expected) 我应该在哪里“放”同步方法才能在超时过后获得负面exception? UPD:对于有兴趣的人请查看以下链接: http://www.elabs.se/blog/53-why-wait_until-was-removed-from-capybara https://github.com/jnicklas/capybara/blob/master/lib/capybara/node/base.rb#L44

在使用RSpec测试控制器时模拟CanCan授权

这是我要测试的控制器: class UsersController < ApplicationController load_and_authorize_resource def index @users = User.all respond_to do |format| format.html # index.html.erb format.json { render json: @users } end end def show @user = User.find(params[:id]) respond_to do |format| format.html # show.html.erb format.json { render json: @user } end end #other actions here end 如您所见,我使用CanCan方法load_and_authorize_resource因此我为RSpec编写了一个ControllerHelper: # spec/support/controller_spec.rb module ControllerHelper def should_authorize(action, […]