shoulda

Rails 4 - undefined method 'authenticate' for nil:NilClass

爱⌒轻易说出口 提交于 2019-12-11 03:07:05
问题 I'm working on a course building a social network using Rails 4 & Devise. I'm stuck trying to get a course to pass, this is the error I'm getting: 1) Error: UserFriendshipsControllerTest#test_: #new when not logged in should get redirected to the login page. : NoMethodError: undefined method `authenticate!' for nil:NilClass test/controllers/user_friendships_controller_test.rb:8:in `block (3 levels) in <class:UserFriendshipsControllerTest>' This is my users_friendships_controller_test.rb :

Test Driven Development - Should I test database columns and indexes?

怎甘沉沦 提交于 2019-12-10 17:55:59
问题 I am new to TDD. I find that the shoulda gem has the ability to test if a column exists for a database entity as well as the ability to test its indexes. But is it necessary to include testing of columns and indexes in my test suite? Will I need to be concerned with a potential deletion of any of the columns and indexes during development? 回答1: Don't test database columns ; that's just testing implementation. Don't test implementation, test behavior. Do test functionality that uses the

How to test conditional validation with rspec-rails 3.0.0 and shoulda-matchers 2.5.0

大憨熊 提交于 2019-12-10 16:44:56
问题 I'm running a Rails 4 app with rspec-rails 3.0.0 and shoulda-matchers 2.5.0, and I've got an Event model that has some conditional validations like so: class Event < ActiveRecord::Base validates :name, :location, :time, :city, :zipcode, :user, :state, presence: true validates :post_text, presence: true, if: :happened? validates :pre_text, presence: true, unless: :happened? belongs_to :community belongs_to :user belongs_to :state def happened? (self.time < Time.now) ? true : false end end My

Capybara issue: @request must be an ActionDispatch::Request

房东的猫 提交于 2019-12-10 16:01:30
问题 I'm having problems making Capybara work with Rails. Just testing that suposedly interesting test thing. OK, in the attached code there are a couple of equivalent tests. The first one is made with shoulda-context + Test::Unit that comes with Rails. The second test is made with capybara and shoulda-context too. require 'integration_test_helper' class UsersTest < ActionDispatch::IntegrationTest fixtures :all context "signup" do context "failure" do setup do @attr = { :name => "", :email => "",

Shoulda: Test validates_presence_of :on => :update

倖福魔咒の 提交于 2019-12-10 14:28:21
问题 I'm using Shoulda in combination with Test::Unit on one of the projects I work on. The issue I'm running into is that I recently changed this: class MyModel < ActiveRecord::Base validates_presence_of :attribute_one, :attribute_two end to this: class MyModel < ActiveRecord::Base validates_presence_of :attribute_one validates_presence_of :attribute_two, :on => :update end Previously, my (passing) tests looked like this: class MyModelTest < ActiveSupport::TestCase should_validate_presence_of

shoulda matchers should validate_uniqueness_of failing with scope

你。 提交于 2019-12-08 15:24:15
问题 I have 3 models, user, game, and player. There's basically a many to many relationship between users and games, with players as the join table, except players have other info so it has its own model. A player needs a unique combo of game id and user id, so I tried to say in player: validates_uniqueness_of :user_id, :scope => :game_id and then in my spec, I said (using shoulda matchers): it { should validate_uniqueness_of(:user_id).scoped_to(:game_id)} here are the relationships the player

Rspec validates_uniqueness_of test failing with additional validation errors

和自甴很熟 提交于 2019-12-08 08:45:36
问题 I have 2 scoped uniqueness validations on my answer model, I am trying to test these using Rspec and the respective shoulda matchers. As seen in the test trace below, the uniqueness validations message is present in the errors array however, there are also 2 and sometimes 3 other errors present; "body can't be blank (nil)", "body is too short (minimum is 10 characters) (nil)", "user_id can't be blank (nil)" . I'm not sure where they are coming from, as body and user attributes are set

How can I generate a report that shows me my slowest running tests in Rails 3.2, Ruby 1.9?

故事扮演 提交于 2019-12-07 11:36:28
问题 I know that RSpec has the --profile option, but I'm only using MiniTest/shoulda for my current project. 回答1: You can use minitest-reporters for this purpose. This gem provide multiple reporters to see output of your tests. Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new Spec reporter shows the time which each test take to run. It shows the time on console, not as a report. 回答2: You can just use: rake TESTOPTS="-v" right out of the box. eg: rake TESTOPTS="-v" test:controllers I

Shoulda/RSpec: Make sure that validation message “xxx” is on :base

*爱你&永不变心* 提交于 2019-12-06 16:00:23
问题 I'm still pretty confused about what is magic behind stuff like it { should have(1).error_on(:base) } and what's a specific Shoulda matcher. I'd like to make sure that :base contains the error message "xxx", so how should I do this? it "should contain error message 'xxx'" do contact.valid? contact.errors[:base].should include('xxx') end Is this "the way to go", or is there a better one? Thanks. 回答1: Right, it's looking good. Inline rspec tests are using subject. You could rewrite your test

Unable to test should belong_to, missing id foreign key on Rails

ぐ巨炮叔叔 提交于 2019-12-06 05:13:26
Hello I have been looking for a way to test model relationship and stumble upon should gem shoulda (3.5.0) shoulda-context (1.2.1) shoulda-matchers (2.8.0) Unfortunatly I tried to test a simple example with rspec describe Region do it "should have a city" do should belong_to(:city) end end and I always get a message of Region should have a city Failure/Error: should belong_to(:city) Expected Region to have a belongs_to association called city (Region does not have a city_id foreign key.) # ./spec/models/region_spec.rb:5:in `block (2 levels) in <top (required)>' I though that there was