rspec

Implicit using subject in rspec example doesn't work

匆匆过客 提交于 2019-12-25 01:55:47
问题 I'm going to test the model concern get_uniq_id method. app/models/concerns/id_generation.rb module IdGeneration extend ActiveSupport::Concern module ClassMethods def get_uniq_id id = '' loop { id = generate_id break unless Ticket.find_by_token(id) } id end def generate_id id = [] "%s-%d-%s-%d-%s".split('-').each{|v| id << (v.eql?('%s') ? generate_characters : generate_digits)} id.join('-') end def generate_digits(quantity = 3) (0..9).to_a.shuffle[0, quantity].join end def generate_characters

Exception Exception RSpec

℡╲_俬逩灬. 提交于 2019-12-25 01:45:54
问题 I have a method depart(plane) which takes an error fail "The plane can't set off because it is stormy" if @weather.stormy? . This is defined in my airport class class Airport attr_accessor :planes, :landed, :weather def initialize(weather = Weather.new) #plane has no location when initialized @landed = nil @planes = [] @weather = weather end def land(plane) fail "You can't land this plane again!" if @landed == true @planes << plane @landed = true end def depart(plane) fail "The plane has

testing with rspec codeschool level 5 challenge 4

一世执手 提交于 2019-12-25 01:45:41
问题 Here is the base question for the test: We've changed the code below so we're mocking ZombieMailer.tweet method instead of the entire method. There is still more to do to make this example work. First, finish the let(:mail) statement below by creating a stub with a deliver method that returns true. Then update the mock so the call to the tweet method returns the mail stub you created. Unfortunately I can not alter the models, question, or mailer. Models: # tweet.rb class Tweet < ActiveRecord:

grape api ignores PUT/POST params

微笑、不失礼 提交于 2019-12-25 00:58:25
问题 I am building a grape api for a rails app. I am testing it with rspec request specs. I have encountered the problem when making a post route like this: resources :events do segment '/:event_id' do resources :tickets do post do event = current_user.events.find params[:event_id] ...#do sth with event using params[:tickets_ids] the corresponding spec: it "should should return the JSON hash of validated tickets" do post "/api/mobile/#{version}/events/#{event.id}/tickets/", { tickets_ids: [1,2] },

RSpec with devise - only the first test is logged in

本小妞迷上赌 提交于 2019-12-25 00:56:10
问题 I'm trying to write a rspec feature test for a page where the user must be logged in with devise. RSpec.feature "User sees items" do before(:context) do .. login_as(@user, :scope => :user) end after(:each) do Warden.test_reset! end it "sees 3 items" do create_items 1, 2 visit root_path expect(page).to have_css("#total", text: "3") end it "sees 7 items" do create_items 3, 4 visit root_path expect(page).to have_css("#total", text: "7") end end In my rails_helper I'm including include Warden:

Rails 5 ActiveStorage How to wait for all threads to finish

与世无争的帅哥 提交于 2019-12-25 00:34:46
问题 First, let me describe the problem. I'm using RSpec to run a script to generate some test data. RAILS_ENV=development rspec spec/dev/food/food_upload.rb The script looks like this. image = fixture_file_upload(Rails.root.join('spec', 'resources', 'test2.png'), 'image/png') (0..200).each { food = FactoryBot.build :food p "Loading #{food.name}" expect { post :upload, params: { "foodUpload_foodName"=> food.name, "foodUpload_image"=> image } }.to change(Food, :count).by(1) .and change(Image,

How to write a rspec test for edit user information function and change password function?

ⅰ亾dé卋堺 提交于 2019-12-24 21:37:26
问题 Does anyone knows how can I write a rspec test for these two functions? The update function is used to allow user to edit their information and update. The second function is used to change user's password with requiring user's old password. def update @user = current_user if @user.update_attributes(user_params) head :no_content else render_error(:unprocessable_entity, "Failed to update information") end end def change_password @user = current_user if @user.authenticate(params[:password])

Why don't I understand the Rails validates presence from tutorial?

[亡魂溺海] 提交于 2019-12-24 19:27:32
问题 I am following a Rails tutorial, and we created a user model, then were testing it with RSpec. In the spec/model/user_spec.rb we have: require 'spec_helper' describe User do before do @user = User.new(name: "Example User", email: "user@example.com") end subject { @user } it { should respond_to(:name) } it { should respond_to(:email) } it { should be_valid } describe "when name is not present" do before { @user.name = " " } it { should_not be_valid } end end When running the test it fails.

Specifying minimum length when using FFaker::Internet.user_name

ぐ巨炮叔叔 提交于 2019-12-24 17:07:35
问题 I have a spec that keeps failing because FFaker::Internet.user_name generates a word that is less than 5 characters. How do I specify a minimum length in this stmt: username { FFaker::Internet.user_name } 回答1: From what I see you try to use FFaker in your factory. Why overcomplicate things for your specs, when you could define sequence sequence(:username) do |n| "username-#{n}" end But the question is valid and you may have some legitimate needs to use ffaker, and there are many ways to do it

RSpec & Rubocop / Ruby Style Guide

烈酒焚心 提交于 2019-12-24 16:19:54
问题 when telling the rails scaffold controller to generate rspec tests the generated files don't follow the Ruby Style Guide and thus rubocop throws errors on these. Only a couple of "cops" fail. Style/HashSyntax, Style/StringLiterals, Style/MethodCallParentheses, Style/SpaceInsideHashLiteralBraces, Style/Blocks. I do understand that some of these cops are just style preferences like for example Style/StringLiterals (Prefer single-quoted strings when you don't need string interpolation or special