Tag: rspec

Rake不会吞下RSpec消息输出

我的规范提供了我所希望的覆盖范围,但是,在rspec输出中显示以下2条消息: rake resque:scheduler rake environment resque:work 如何在规范运行期间吞下这些,以便它们不会搞砸我的nyancat格式化程序? 规格 describe ‘database rake task’ do include_context ‘rake’ let(:task_paths) { [‘tasks/heroku’] } before do invoke_task.reenable end # rubocop:disable all describe ‘myapp:heroku’ do context ‘:setup’ do context ‘:secrets’ do let(:task_name) { ‘myapp:heroku:setup:secrets’ } context ‘with env’ do it ‘works’ do expect(File).to receive(:exist?).with(‘.env’).and_return(true) expect_any_instance_of(Object).to receive(:system).with(‘heroku config:push –remote production’).and_return(true) expect { […]

如何使用rspec和factory测试模型的方法

我是铁杆上的新手,我必须用’Rspec’,’shoulda’和’factory girl’gem为现有的rails应用程序编写测试。 我可以测试非特定的测试,例如validates_presence_of:与’sholda’匹配器的东西。 但我想测试模型中的方法。 我可以想象我需要做什么,但我不能写作。 这就是我所说的一个例子: . . . context ‘is editable if project is not started’ do setup do @brief=Factory(:brief) @started_project=Factory(:project_started, :brief => @brief) @brief_have_no_project=Factory(:brief) end specify “with editable brief” do @brief.brand_info = ‘bla bla bla’ #change brand info, this is impossible #i can’t compose this section :S end specify “with non-editable brief” do […]

在FactoryGirl中查找_or_initialize_by

我想知道FactoryGirl中是否存在find_or_initialize_by的等价物来解决以下问题: 目标是该模型使用两个具有相同国家/地区的表。 我不想在国家使用序列(正如我在电子邮件中找到的那样)。 Country上有一个唯一性约束,但我的主要问题是,当我调用FactoryGirl.create(:click)时,它会创建两次相同的Country记录 因此,validation在测试中失败。 Rspec的: # models/click_spec.rb describe Click do it “should have a valid constructor” do FactoryGirl.create(:click).should be_valid end end 工厂: # factories/countries.rb FactoryGirl.define do factory :country do name “United States” slug “us” end end # factories/offers.rb FactoryGirl.define do factory :offer do association :country, factory: :country # Other columns end end # factories/users.rb […]

访问其他类的变量

我在上一篇文章( rspec测试中的未定义方法 )上得到了一些很好的帮助,但我只是想寻求更多的帮助。 我有一个rspec集成规范,我基本上需要更改代码以获得所需的结果。 我不能改变规范,因为它是练习的一部分。 let(:user) { User.new(voucher) } context ‘no voucher’ do let(:voucher) { nil } it ‘should bill default price all the time’ do user.bill expect(user.orders[0].billed_for).to eql 6.95 … … end end context ‘vouchers’ do describe ‘default vouchers’ do let(:voucher) { Voucher.create(:default, credit: 15) } it ‘should not bill user if has a […]

如何使用RSpec测试Rails 4应用程序并从Railscast#53(修订版)中自定义exception处理?

使用此Railscast剧集 , ActiveRecord::RecordNotFound错误最终会重定向到ErrorsController和/app/views/errors/404.html.erb视图。 但是,我在我的一个测试中有以下内容(Item belongs_to List): scenario “item cannot be created without a list” do visit new_item_path # Because it’s not the proper nested new_list_item_path(@some_list) path, # @list is not created in the ItemsController. page.should have_content(‘Not Found’) page.status_code.should be 404 end 但是,测试失败并出现以下情况: Failure/Error: visit new_item_path ActiveRecord::RecordNotFound: Couldn’t find List without an ID 因此控制器在需要列表时正常工作,但测试失败,因为它没有考虑应该将用户发送到404的自定义exception处理。 如何正确测试重定向?

检测rspec或黄瓜是否正在运行的简便方法?

我正在检查某些地方是否正在测试模式下执行代码 Rails.env.test? 我想更进一步,检查是否是Cucumber或Rspec执行代码,以便微调i18n的一些方法(我确实希望在Cucumber上下文中运行不同,但不在Rspec上下文中) 有没有办法做到这一点?

名为route的Rails无法识别

我已经开始研究着名的Rails教程的第8章了 。 我想我密切关注说明并定义了以下路线: SampleApp::Application.routes.draw do resources :users resources :sessions, only: [:new, :create, :destroy] root ‘static_pages#home’ match ‘/signup’, to: ‘users#new’, via: ‘get’ match ‘/signin’, to: ‘sessions#new’, via: ‘get’ match ‘/signout’, to: ‘sessions#destroy’, via: ‘delete’ match ‘/help’, to: ‘static_pages#help’, via: ‘get’ match ‘/about’, to: ‘static_pages#about’, via: ‘get’ match ‘/contact’, to: ‘static_pages#contact’, via: ‘get’ 会话控制器(/controllers/sessions_controller.rb)定义如下: class SessionsController < […]

RSpec with devise – 仅记录第一个测试

我正在尝试为用户必须使用设备登录的页面编写rspecfunction测试。 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 在我的rails_helper我include Warden::Test::Helpers 。 但只有我的第一次测试值3用户登录。第二次测试有设计错误You need to […]

Rspec:如何在私有方法中引发exception测试?

在私有方法中测试exception时出错。 如何在私有方法中引发exception,该方法是从公共方法调用的? 上市 def public_method private_method end 私人的 def private_method tries = 0 begin raise Product::StaleObjectError.new(“Product is changed while you were editing”) if stale_object? // Do some work raise Exception.new(“Total amount used is greater than approved”) if total_approved e if tries < MAX_RETRIES tries += 1 sleep(1 + tries) reload retry else raise Product::StaleObjectError("Product is […]

使用Rspec测试Mailchimp 3.0和Gibbon 2.x.

我有一个rails 4.2应用程序,通过MailChimp使用Gibbon gem进行简报注册。 这是我的初始化程序: Gibbon::Request.api_key = ENV[‘MAILCHIMP_API_KEY’] Gibbon::Request.timeout = 15 Gibbon::Request.throws_exceptions = false 以下是user.rb中的相关方法: # returns the mailchimp member if one exists for @user.email def mailchimp_user gb = Gibbon::Request.new(api_key: ENV[‘MAILCHIMP_API_KEY’]) gb.lists(ENV[‘MAILCHIMP_LIST_ID’]).members(Digest::MD5.hexdigest(“#{self.email.downcase}”)).retrieve rescue Gibbon::MailChimpError => e return nil, :flash => { error: e.message } end def mailchimp_member_id if self.mailchimp_user.kind_of?(Array) return nil elsif self.mailchimp_user.kind_of?(Hash) self.mailchimp_user[“id”] end end […]