Tag: 测试

使用子项存在validationFactory Girl创建父项和子项

有一个包含许多旅行的发票的项目。 新的故事以我的方式要求发票必须旅行。 我添加了一个validationvalidates :trips, presence: true trip validates :trips, presence: true但是现在它已经炸毁了我的一些测试,因为FactoryGirl在创建相关行程之前试图保存发票。 FactoryGirl.define do factory :invoice do sequence(:invoice_id) { SecureRandom.uuid} merchant amount 100.00 item_count 1 paid false currency “GBP” invoice_type “pre-flight” service_rendered false cancelled false after(:create) { |object| create(:trip, invoice_id: object.invoice_id)} end end 我该怎么做才能创建这些对象。 最好是在工厂级别,因为有许多测试利用这种行为(并且目前由于它而失败)。 这似乎是测试级别的一个很好的解决方案。 更新仍然努力让我的测试现在变绿。 42使用以下代码进行测试时出错。 validation失败:旅行不能为空 我在FactoryGirl代码中的当前更新行 before(:create) { |object| object << build(:trip, […]

Stub(…)收到意外消息(…)(没有args)

我尝试使用RR编写测试。 我需要的是模型对象的存根。 describe ApplicationController do subject(:application_controller) { ApplicationController.new } let(:messages) { [‘a1’, ‘a2’, ‘a3’ ] } let(:model) { Object.new } it ‘should copy errors to flash’ do stub(model).error_messages { messages } flash[:error] == nil subject.copy_errors_to_flash(model) flash[:error].should == messages end end 我得到的是 ApplicationController should copy errors to flash Failure/Error: stub(model).error_messages { messages } Stub # received […]

如何运行一次capybara命令,然后运行一些测试

我有给定的测试代码: describe ‘A new user’, js: true do before do @new_user = Fabricate.build(:user) end it ‘should sign up’ do #login code visit ‘/’ click_link ‘Login’ fill_in ‘user[email]’, :with => @new_user.email fill_in ‘user[password]’, :with => @new_user.password click_button ‘Login now’ #login code end page.should have_content(“Hello #{@new_user.first_name}!”) current_path.should == dashboard_path end it ‘should receive a confirmation mail’ do […]

paypal v.zero测试nonce失败,没有错误消息

我正在尝试测试paypal v.zero与Braintree ruby​​ gem中提供的测试nonce的集成。 注意 – 遵循paypal集成指南 。 我可以通过手动完成结账流程(输入测试paypal信息,提交订单)生成支付nonce来运行成功的授权,但Braintree提供的测试nonce失败,错误结果不包括任何错误消息“处理器拒绝“。 测试代码: braintree_gateway = Braintree::Gateway.new( access_token: Figaro.env.BRAINTREE_ACCESS_TOKEN ) result = braintree_gateway.transaction.sale( amount: “10.00”, payment_method_nonce: Braintree::Test::Nonce::PayPalOneTimePayment ) 结果: #<Braintree::ErrorResult params:{…} errors: transaction: #<Braintree::Transaction id: "ddz5mc", type: "sale", amount: "10.0", status: "processor_declined", created_at: 2016-04-15 02:55:13 UTC, credit_card_details: #, customer_details: #, subscription_details: #, updated_at: 2016-04-15 02:55:14 UTC>> Braintree测试nonce不应该用于paypal集成方法吗? 如果是这样,有没有人知道另一种方法来生成paypal支付nonces用于测试目的(除了手动完成结账过程)? 提前致谢。

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 […]

RSpec – 测试是否使用之前定义的块调用块

我最近问过如何在RSpec中测试是否调用了一个块,并且该问题的答案似乎在一个简单的情况下起作用。 问题是当块的初始化更复杂时。 然后它在before完成并在上下文中通过许多不同的测试重用,其中一个测试是否评估了块。 看例子: context “the node definition using block of code” do before do @n=node do # this block should be called end # some more complex setup concerning @n end it “should call the block” do # how to test it? end # here a bunch of other tests using @n end 在这种情况下,具有副作用改变局部变量值的解决方案不起作用。 […]

无法通过Post在Rspec中创建模型

我正在尝试在Rspec中创建一个用户: require “rails_helper” RSpec.describe UsersController, type: :controller do describe “POST #index” do it ‘should create a user’ do post :create, {name: “name1”, age: 25}.to_json, format: :json # …………. 但我每次都会收到validation错误: name can’t be blank, age can’t be blank UserController接受json: def create @user = User.new(params1) if @user.save render json: @user else render json: @user.errors, status: :unprocessable_entity end […]

FactoryGirl工厂带有Trait(s),它返回带有弦乐键的Hash

我正在尝试使用FactoryGirl来构建一个返回如下内容的Hash : => {“3″=>”1”, “6”=>”Word”} 我已经接近但不是100%那里…… 我试过的第一个工厂定义看起来像这样: factory :faqtory, class: Hash do |f| f.ignore do fake_word Faker::Lorem.word end f.sequence(1.to_s) { |n| n } f.send(2.to_s, fake_word.to_s.capitalize) initialize_with { attributes.stringify_keys } end 不幸的是,这不起作用: 1.9.3p448 :001 > FactoryGirl.build :faqtory ArgumentError: Trait not registered: fake_word 在那之后不起作用我假设对fake_word的调用需要在一个块中,但这没有任何区别。 有什么建议?

Sinatra测试总是404′

我有一个非常简单的Sinatra应用程序,我在测试时遇到了问题。 基本上,当我从浏览器中的测试中知道请求正常工作时,每个请求测试都会返回404。 关于问题可能是什么的任何想法? test_helper.rb中: ENV[“RACK_ENV”] = ‘test’ $: << File.expand_path(File.dirname(__FILE__) + '/../lib') require 'app' Sinatra::Synchrony.patch_tests! class Test::Unit::TestCase include Rack::Test::Methods end app_test.rb require ‘test_helper’ class AppTest < Test::Unit::TestCase def app @app ||= Sinatra::Application end def test_it_says_hello get "/" assert_equal 200, last_response.status end end app.rb $: << 'config' require "rubygems" require "bundler" ENV["RACK_ENV"] ||= "development" Bundler.require(:default, ENV["RACK_ENV"].to_sym) […]

gem如何提供Cucumberfunction和步骤定义?

我有两个Rails项目(A和B),其中包括以下gem: #project A gem “green_theme”, :path => “~/dev/themes/green_theme” gem “devel_tasks”, :path => “~/dev/themes/devel_tasks” #project B gem “red_theme”, :path => “~/dev/themes/red_theme” gem “devel_tasks”, :path => “~/dev/themes/devel_tasks” A和B两个项目都具有function和步骤的function目录。 红色和绿色这两个主题应该具有共同的function,如登录/注销按钮,html元标记等。 我需要做的是将共同主题相关的function和步骤放在devel_tasks gem中。 这样我就可以共享测试并使用它们来检查给定模板是否符合标准。 测试我需要做的一个简单方法是使用示例Cucumberfunction创建一个新的Rails项目,然后尝试运行“cucumber examples / i18n / en / features”。