rspec

yield to a block using rr

房东的猫 提交于 2019-12-20 04:12:10
问题 I'm trying to test the following code using rr : response = RestClient.get(url, {:params => params}){|response, request, result| response } In vanilla rspec , you would do something like this: RestClient.should_receive(:get).with(url, {:params => params}).and_yield(response, request, result) How would I do the same with rr ? Setup: let(:url) { "http://localhost/" } let(:params) { {:item_id => 1234, :n => 5} } let(:response) { Object.new } let(:request) { Object.new } let(:result) { Object.new

yield to a block using rr

送分小仙女□ 提交于 2019-12-20 04:12:02
问题 I'm trying to test the following code using rr : response = RestClient.get(url, {:params => params}){|response, request, result| response } In vanilla rspec , you would do something like this: RestClient.should_receive(:get).with(url, {:params => params}).and_yield(response, request, result) How would I do the same with rr ? Setup: let(:url) { "http://localhost/" } let(:params) { {:item_id => 1234, :n => 5} } let(:response) { Object.new } let(:request) { Object.new } let(:result) { Object.new

Why Rails instance method can be used as class method in rspec

↘锁芯ラ 提交于 2019-12-20 03:22:54
问题 I found a snippet in an article about sending mails in a Rails application: class ExampleMailerPreview < ActionMailer::Preview def sample_mail_preview ExampleMailer.sample_email(User.first) end end in this link: http://www.gotealeaf.com/blog/handling-emails-in-rails. I do not know why the method: sample_email() , which in my mind should be an instance method, can be accessed like class method here as ExampleMailer.sample_email() . Can anyone explain? 回答1: It's not an rspec thing, it's an

Checking checkboxes with Capybara

∥☆過路亽.° 提交于 2019-12-20 00:59:10
问题 Using Capybara I cannot for the life of me select a checkbox on my form. In my request spec I've tried: check("First Name") page.check("First Name") page.check("pickem_option_ids_10") find(:css, "#pickem_option_ids_11[value='11']").set(true) find(:css, "#pickem_option_ids_11").set(true) Snippet from my form: <div class="control-group check_boxes optional"> <label class="check_boxes optional control-label">Options:</label> <div class="controls"> <label class="checkbox"> <input class="check

RSpec — test if method called its block parameter

纵饮孤独 提交于 2019-12-19 21:09:40
问题 I have a method that takes block of code as an argument. The problem is: how to test using RSpec if this method called the block? The block may be evaluated in any scope the method needs, not necessarily using a yield or block.call . It be passed to another class, or evaluated it in an anonymous class object or somewhere else. For the test to pass it is enough to evaluate the block somewhere as a result of the method call. Is there a way to test something like this using RSpec? See also this

How do I test ajax POST request with RSpec?

风流意气都作罢 提交于 2019-12-19 12:25:17
问题 I just want to test ajax request on controller spec. The product code is below. I'm using Devise for authentication. class NotesController < ApplicationController def create if request.xhr? @note = Note.new(params[:note]) if @note.save render json: { notice: "success" } end end end end And spec is below. describe NotesController do before do user = FactoryGirl.create(:user) user.confirm! sign_in user end it "has a 200 status code" do xhr :post, :create, note: { title: "foo", body: "bar" },

absurdly slow running rspec test

无人久伴 提交于 2019-12-19 11:56:19
问题 I have this very simple and short test which takes a 80 seconds to run, any advice on speed it up? require 'spec_helper' describe "Battles" do let(:character) { (FactoryGirl.create :character) } describe "Battle button" do it "redirects to battle screen" do character.init("fighter") click("Battle") expect(page).to have_content character.name end end end Gemfile source 'https://rubygems.org' ruby '2.0.0' gem 'pg', '0.15.1' gem 'rails', '4.0.2' gem 'bcrypt-ruby', '~> 3.1.2' gem 'twitter

Basic Devise spec with I18N

不问归期 提交于 2019-12-19 11:29:39
问题 I'm new to RSpec, and trying to write a simple test that shows Devise is working. I've picked a random page and want to write a test that shows that a non logged-in user is re-redirected to /users/sign_in. describe OrgsController do describe "GET index when not logged in" do it "redirects to new_user_session_path" do user = FactoryGirl.create(:user) user.confirm! sign_in user sign_out user get :index response.should redirect_to new_user_session_path end end before(:each) do user = FactoryGirl

RSpec controller specs fail after adding authorization to Rails4 application

断了今生、忘了曾经 提交于 2019-12-19 10:56:13
问题 Following Railscast #385 & #386 I have added authorization to my Rails 4 application (rails 4.2.6, rspec-rails 3.4.2). After adding authorization, all of my controller specs fail. My feature specs still pass because in the spec I login as admin to perform actions that are not allowed visitors (:edit, :update...). If I make all actions allowed to a visitor my controller specs pass. However, any action that only the admin can perform will fail in the controller spec. The scaffold generator

how to test STDIN with RSpec

China☆狼群 提交于 2019-12-19 09:45:32
问题 Ok, need help working out a test. I want to test that this class receives a letter "O" and that when called the "move_computer" method returns WHATEVER the person enters on the cli. my mental subprocessor tells me this is a simple assign a variable to something to hold the random human input at STDIN. Just not getting it right now...anyone point me in the right direction? here is my class... class Player def move_computer(leter) puts "computer move" @move = gets.chomp return @move end end my