rspec

Rails - How to stub a certain response time for a stubbed / fake http or ajax response

℡╲_俬逩灬. 提交于 2019-12-25 07:28:26
问题 I want to test a feature that in my app sends the usera custom message when a Rails UJS/ajax times-out. The code to timeout the Rails UJS request is itself on the app: $.rails.ajax = function(options) { if (!options.timeout) { options.timeout = 10000; } return $.ajax(options); }; When observing on chrome dev tools what happened when it timed out on my local dev mode, I notice the code status is strangely 200 but as it "times out", my message is indeed displayed to the user. on('ajax:error'

setting up objects for model testing with factory_girl

我是研究僧i 提交于 2019-12-25 07:26:38
问题 I'd like to test the scopes below but don't know hot to setup test objects properly. I don't even know if I should create those in factory with some associations or using let/local vars. task.rb belongs_to :assigner, class_name: "User" belongs_to :executor, class_name: "User" scope :completed, -> { where.not(completed_at: nil) } scope :uncompleted, -> { where(completed_at: nil) } scope :alltasks, -> (u) { where('executor_id = ? OR assigner_id = ?', u.id, u.id) } scope :between, -> (assigner

How to create factories with attr_accessible?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-25 06:58:29
问题 How to deal with factories and attr_accessible ? My example: # model class SomeModel attr_accessible :name, full_name, other_name end #spec require 'spec_helper' describe "test" do it do create(:some_model, name: "test name", user: User.first) #factory end end #error ruby(17122,0x12a055000) malloc: *** error for object 0x8: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug I think the error is because user_id is not in attr_accessible attributes. 回答1:

rspec controller spec matchers

喜你入骨 提交于 2019-12-25 06:58:05
问题 With which matcher and how can I test if the @post_comment and @post_comment.user is properly assigned? expect(assigns(:post_comment)).to be_a_new(PostComment) is not working here. UPDATE: With the following setup I also get the following error. What should I change to be able to test the invalid attrs? Posts::PostCommentsController when user is logged in POST create with invalid attributes doesn't save the new product in the db Failure/Error: @post_comment.save! ActiveRecord::RecordInvalid:

Testing Datamapper models with RSpec

China☆狼群 提交于 2019-12-25 05:29:13
问题 I'm testing a Sinatra application, which is using DataMapper, with RSpec. The following code: it "should update the item's title" do lambda do post "/hello/edit", :params => { :title => 'goodbye', :body => 'goodbye world' } end.should change(Snippet, :title).from('hello').to('goodbye') end Results in this error: title should have initially been "hello", but was #<DataMapper::Property::String @model=Snippet @name=:title> I can of course hack this by removing the lambda and only checking if:

Testing the draft attribute of this post model (Rspec + Capybara)

淺唱寂寞╮ 提交于 2019-12-25 05:25:51
问题 I want to write a test using Rspec and Capybara. I have a Post model and I want to add a draft attribute to it. If the user checks that field the post is saved as draft ( draft = true ). Only posts that have he attribute draft = false will show in the post index page. Posts with draft = true will only show in the page of the user who created that post. I've never made a Rspec + Capybara test in my life. So I was wondering if someone could tell me how to start or give me an example. Thanks in

rspec failing validations

可紊 提交于 2019-12-25 05:25:18
问题 i have a meeting.rb and user.rb model meeting.rb class Meeting < ActiveRecord::Base attr_accessible :intro, :proposed_date, :proposed_location validates :requestor_id, presence: true validates :requestee_id, presence: true belongs_to :requestor, class_name: "User" belongs_to :requestee, class_name: "User" end user.rb class User < ActiveRecord::Base attr_accessible :name, :email, :password, :password_confirmation has_secure_password has_many :sent_meetings, :foreign_key => "requestor_id",

Testing cancan abilities and getting MassAssignmentSecurity::Error

纵然是瞬间 提交于 2019-12-25 05:18:31
问题 I have implemented cancan and would like to test abilities as recommended on the cancan wiki. I trying to replicate "user can only destroy projects which he owns." spec/models/ability_spec.rb: require "cancan/matchers" require 'spec_helper' describe Ability do context "user is investigator" do it "user can only destroy projects which he owns" do user = FactoryGirl.create(:user) ability = Ability.new(user) ability.should be_able_to(:destroy, Project.new(:user => user)) end end end However I

Rake does not swallow RSpec message output

杀马特。学长 韩版系。学妹 提交于 2019-12-25 04:57:09
问题 My spec provides coverage like I had hoped, however, the following 2 messages are displaying in the rspec output: rake resque:scheduler rake environment resque:work How do I swallow these during spec runs so they do not screw up my nyancat formatter? Spec 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) {

Optimising Rspec Tests to Avoid Repeating Complex Setup Proceedures

久未见 提交于 2019-12-25 04:56:07
问题 So here's my problem: I am writing unit tests for my Rails models and I have a whole set of examples that each require the same setup in order to run. If I'm not mistaken, the usual way to set things up the same way for multiple RSpec tests is to use a before(:each) block, like this: describe Model do before(:each) do # Complex setup end # Examples end Unfortunately the set of examples which needs this setup is starting to get rather large, and completing this complex setup proceedure for