Tag: rspec

如何用RSpec测试这种破坏行为?

在我的Rails应用程序中,如果user想要删除自己的帐户,他首先必须在我的terminate视图中输入他的密码: :delete do |f| %> 这是我的UsersController : def terminate @user = User.find(params[:id]) @title = “Terminate your account” end def destroy if @user.authenticate(params[:user][:password]) @user.destroy flash[:success] = “Your account was terminated.” redirect_to root_path else flash.now[:alert] = “Wrong password.” render :terminate end end 问题是我似乎找不到用RSpec测试这个的方法。 我有的是这个: describe ‘DELETE #destroy’ do before :each do @user = FactoryGirl.create(:user) end context “success” […]

作为一个人,我可以查看RSpec渲染的视图,如果,那么如何?

我希望能够在我的一个视图规范中看到请求路径的输出。 Rspec可以访问渲染的视图,但我想自己访问它。 有谁知道如何实际查看渲染的视图?

重新加载不在rspec中工作的对象

我试图用以下代码测试控制器方法: it “should set an approved_at date and email the campaign’s client” do @campaign = Campaign.create(valid_attributes) post :approve, id: @campaign.id.to_s @campaign.reload @campaign.approved_at.should_not be(nil) end 但是,当我运行此测试时,我收到以下错误: Failure/Error: @campaign.reload ActiveRecord::RecordNotFound: Couldn’t find Campaign without an ID 当我在rails控制台中运行类似的行时,重新加载工作并且值设置为我需要它。 当我在rspec测试中运行代码时,为什么不重新加载?

如何控制器测试rails 4引擎重定向回main_app路由

我在控制器测试我创建的rails引擎时遇到了一些麻烦。 我遇到的问题是我似乎无法让rspec使用main_app路由。 例如,我正在编写以下控制器测试。 如果不是main_app.root_path返回nil,测试将通过。 我在控制器规范中的代码是: context “successful session creation” do let(:user) {FactoryGirl.create :user} it ‘sets user id session’ do get :create, use_route: :authenticatable, email: user.email, password: ‘password’ expect(session[:user_id]).to eq user.id end end end 在我的控制器中,我有: def create user = Authenticatable::User.find_by(email: params[:email]) if user && user.login(params[:password]) session[:user_id] = user.id redirect_to main_app.root_path, notice: “You are signed in!” else […]

对于简单路由,RSpec控制器测试失败

这可能是一个非常基本的错误,但我仍在学习。 =) 我的routes.rb只包括 WebPortal::Application.routes.draw do resources :categories end 如果我理解正确,这应该(以及其他)地图/categories到CategoriesController.index 。 这个控制器看起来像 class CategoriesController < ApplicationController def index end end 相应的视图文件存在,rails服务器可以很好地提供此页面。 但我的RSpec测试 describe CategoriesController do describe “GET :index” do it “should be succesful” do get :index response.should be_succes end end end 失败了 Failure/Error: get :index ActionController::RoutingError: No route matches {:controller=>”categories”} 我在这做错了什么? 编辑: 命令rake routes给出 rake routes […]

设计Rspec注册控制器测试失败,就好像它正在尝试确认电子邮件地址一样

我有以下路线: devise_for :users, :controllers => { :omniauth_callbacks => “users/omniauth_callbacks”, :registrations => ‘users/registrations’, :sessions => “users/sessions” } 和以下控制器测试(registrations_controller_spec.rb): require File.dirname(__FILE__) + ‘/../spec_helper’ describe Users::RegistrationsController do include Devise::TestHelpers fixtures :all render_views before(:each) do @request.env[“devise.mapping”] = Devise.mappings[:user] end describe “POST ‘create'” do describe “success” do before(:each) do @attr = { :email => “user@example.com”, :password => “foobar01”, :password_confirmation => […]

RSpec :: ExampleGroups :: User :: Validations的未定义方法`validate_presence_of’

我试图通过我的模型规范进行validation,但我无法让它们通过。 我已将所有validation规则放在我的模型文件中 应用程序/模型/ user.rb class User :friendships has_many :plans has_many :memberships, class_name: “PlanMemebership” has_many :notes has_many :replies, class_name: “NoteReply” has_many :upvotes, class_name: “NoteUpvote” validates :first_name, presence: true validates :last_name, presence: true validates :email, presence: true validates :facebook_user_id, presence: true, uniqueness: true validates :facebook_user_token, presence: true, uniqueness: true validates :token, presence: true, uniqueness: true validates :birthday, […]

如何使用rspec测试mandrill api

所以我的客户报告说很多电子邮件都是错误的人,我想编写一些function测试来查找并确保他们收到的电子邮件及其在我的规范中说的内容。 我有mandrill_mailer使用mandril api,在它发出之前我想知道消息是什么。 例如。 创建一个新用户帐户 – >创建用户,然后发送一封欢迎电子邮件。 在设计中,它调用RegistrationMailer.new_registration(resource).deliver,然后发送电子邮件给用户: def new_registration(user) user = User.find_by_email(user[“email”]) mandrill_mail template: ‘new-registration’, subject: ‘Welcome to ContentBlvd!’, to: { email: user[“email”], name: user[“full_name”] }, vars: { ‘first_name’ => user[“full_name”], ‘unsubscribe’ => “#{CONFIG[:protocol]}#{CONFIG[:host]}/unsubscribe?email=#{user.email}” } 结束 在我的邮件程序中,如何测试此邮件对象? 我尝试了ActionMailer :: Base.deliveries,但它返回nil(因为我使用的是mandrill邮件程序…)所以我试过 – MandrillMailer :: TemplateMailer.message但是没有运气….感谢您的帮助。

rails 4 api rspec test http状态码410

我正在尝试构建正确的rspec测试以validation我的410状态代码处理程序是否正常工作。 以下是路线捕获的所有内容: match ‘*not_found’, to: ‘error#error_404’, via: :all 我的简单错误控制器: class ErrorController < ApplicationController def error_404 head status: 410 end end 我目前的rspec: require ‘spec_helper’ describe ErrorController do context “Method #error_404 handling missing routes =>” do it “Should have the 410 status code..” do get :error_404 expect(response.status).to be(410) end end end Rspec错误消息: 1) ErrorController Method #error_404 handling […]

validationRspec和Rails中的布尔值

我很困惑如何validationRspec和Rails中的布尔值。 我理解除了false和nil之外的所有内容在Ruby中都是true的。 但是当我使用MySQL和Rails时,它使用1表示true , 0表示false (如果我的理解是正确的话)。 我有以下型号规格。 我想测试superuser属性的布尔值。 我怎么在这里写规格? 我怎么在这里编写实现代码? 我的规范和实现代码是否特定于特定数据库(如MySQL和PostgreSQL)? require ‘spec_helper’ describe User do before(:each) do @valid_attributes = { :username => “mike”, :password => “super_encryped_password”, :email => “mike@example.com”, :superuser => true } end it “should create a new instance given valid attributes” do User.create!(@valid_attributes) end it “should have true or false for superuser” […]