Tag: rspec

指定明确的’主题’的麻烦?

我正在使用Ruby on Rails 3.0.9和RSpect 2.我试图以下面的方式重构一些spec文件(为了测试更少代码类似的User类对象属性值): describe User do let(:user1) { Factory(:user, :users_attribute_a => ‘invalid_value’) } let(:user2) { Factory(:user, :users_attribute_b => ‘invalid_value’) } let(:user3) { Factory(:user, :users_attribute_c => ‘invalid_value’) } it “foreach user” do [ user1, user2, user3 ].each do |user| subject { user } it “should be whatever” user.should_not be_valid … end end end end […]

如何在rspec示例中存根全局日志记录function

我正在使用一些记录到全局静态日志记录类的代码,例如: GlobalLog.debug(“Some message”) 但是在我的测试中,我不想包含真正的日志,因为它引入了许多不需要的依赖项。 所以我想嘲笑它: describe “some function” do before(:all) do log = double(‘log’) GlobalLog = log log.stub(:debug) end … end 不幸的是,因为在每个示例之后清除了双精度数,所以不允许这样做: https://www.relishapp.com/rspec/rspec-mocks/docs/scope 如果我将before(:all)更改为before(:each) ,代码可以正常工作,但是我收到警告: warning: already initialized constant GlobalLog 这会堵塞我的测试输出,所以我想避免警告。 有清洁的解决方案吗?

an_instance_of Mail ::消息未返回true

根据an_instance_of 匹配器上的这些文档 ,调用an_instance_of是另一种调用instance_of?(Class) 。 当我在控制台中测试时,这工作正常,但在我的规范中它失败了。 在控制台中: m = Mail.new => # m.instance_of?(Mail::Message) => true 失败: 1) IncomingEmailsController should deliver the email with lead info Failure/Error: post :create, to: “me@example.com”, text: “Content”, html: “HTML Content”, from: “email@example.com”, subject: “Jimmy Bean” received :forward_email with unexpected arguments expected: (#) got: (#<Mail::Message:70334278571020, Multipart: true, Headers: , , , >, […]

嵌套模型测试:找不到表’*’错误。

我正在尝试对一个working大型代码库运行RSpec(我对Rails相对较新),但它在这一点上失败了; 我敢打赌它与FactoryGirl定义有关。 模型概述: class User {:approved => true} has_many :friendships, :class_name => “User”, :source => :friend, :through => :friends # … 测试方法: # models/user.rb def add_friend(user_id, friend_id) @friendship = self.friends.new({:user_id => user_id, :friend_id => friend_id}) return false unless @friendship.save end FactoryGirl工厂: FactoryGirl.define do factory :user, :class => User do |f| # … end factory :friend, […]

Rails 3 – validation时间存在

我有一个带有Time属性的模型。 我想检查时间不能为空(更好的选择可能是检查输入是时间,但我不知道如何处理)。 我试过这个validation: # id :integer not null, primary key # school_class_id :integer # meeting_time :time class Meeting { :message => “can’t be empty!” } end 然后我试图在规范中检查这个,这失败了(空的时间还可以,但它不应该是)。 我做错了什么? Rspec的: # id :integer not null, primary key # school_class_id :integer # meeting_time :time require ‘spec_helper’ describe Meeting do before(:each) do @class = FactoryGirl.create( :school_class ) @attr_meeting […]

Rails嵌套委托属性未更新

楷模 class User < ActiveRecord::Base has_one :meta, class_name: SeoMetum, as: :metumable, dependent: :destroy delegate :browser_title, :browser_title=, :meta_description, :meta_description=, to: :meta has_one :collection accepts_nested_attributes_for :collection after_save :save_meta_tags! def save_meta_tags! meta.save end end class Collection < ActiveRecord::Base has_one :meta, class_name: SeoMetum, as: :metumable, dependent: :destroy delegate :browser_title, :browser_title=, :meta_description, :meta_description=, to: :meta belongs_to :user after_save :save_meta_tags! def […]

当have_selector返回成功时,Rspec have_field引发错误

在下面的示例中,3个has_field规范失败,而2个have_selector传递。 describe “without js support”, js: false do subject { page } it { should have_selector “form label[for=’user_password’]”, text: “Password” } it { should have_selector “form input#user_password[name=’user[password]’][type=’password’][required]” } it { should have_field “user_password” } # check by field id it { should have_field “user[password]” } # check by field name it { should have_field “Password” […]

可以使用Nightwatch来测试Rails吗?

我们的创业公司的承包商安装了基于Selenium的Nightwatch测试框架,因为我们的堆栈是React-heavy。 但他告诉我它甚至可以用来测试我们的Rails代码。 相反,一位新的承包商表示,守夜人无法对我们的Rails控制器和模型进行unit testing(这对我来说很有意义)。 谁是对的? 您是否认为第一个程序员只是考虑到我们会进行端到端测试(某些输入导致某些输出),而且我们不需要测试Rails代码的细节? 我怀疑和新承包商断言,如果我们想要成为TDD商店,我们是否需要RSpec或其他一些基于Ruby的测试框架来处理我们的Rails代码?

Rails教程第9.1.1章sign_in无法识别

我正在研究Michael Hartl的教程。 我刚刚将这些测试介绍给user_pages_spec.rb describe “edit” do let(:user) { FactoryGirl.create(:user) } before do sign_in user visit edit_user_path(user) end describe “page” do it { should have_content(“Update your profile”) } it { should have_title(“Edit user”) } it { should have_link(‘change’, href: ‘http://gravatar.com/emails’) } end describe “with invalid information” do before { click_button “Save changes” } it { should […]

rails rspec – (第二次测试)预期响应是,但是

预期的响应是,但是 我的测试有: describe “Link POST #create” do context “with valid attributes” do it “creates a new link” do expect{ post :create, link: FactoryGirl.create(:link, :group => @group) }.to change(Link,:count).by(1) end it “redirects to the new link” do post :create, link: FactoryGirl.create(:link, :group => @group) # response.should redirect_to @link # Link.unscoped.last response.should redirect_to Link.unscoped.last # render_template :show […]